tags:

views:

82

answers:

6

I have always been confused that .e,g in php i have sql statement

$qry = "select * from table where id = $id";

now can i insert "$" directly inside the quotes or i have to use

 $qry = "select * from table where id =".$id." ";

or

 $qry = 'select * from table where id = $id';

or

 $qry = 'select * from table where id = '$id'';

Which is correct

+4  A: 

None of the above, unless $id happens to be SQL escaped already. You'll probably want to use this, assuming you're using MySQL:

$qry = "select * from table where id = '".mysql_real_escape_string($id)."'";

Edit: Okay, that was incorrect. As per the comment on my answer, this should work:

$qry = "select * from table where id = ".(int)$id;
icktoofay
@icktoofay: if id is integer - you need to explicitly cast it to (int) and remove single quotes
zerkms
@zerkms: Augh, I thought that might be the case... I guess I'll edit it.
icktoofay
what is incorrect , my code is working. what if i don't use (int).I haven't used that
Mirage
@Mirage: before using as the parameter of sql query **any** variable should be sanitized. integers should be casted into integers explicitly with (int) modifier, strings should be processed by mysql_real_escape_string. this should be done in **each query**, without any exception.
zerkms
@zerkms If you know that $id is a string integer (i.e., it's a string that contains only numeric characters) the cast is not necessary, and can even be detrimental if the id is bigger than PHP_MAX_INT.
Artefacto
@Artefacto: yep, i know this. but for newbies and small projects it always is enough rule to follow.
zerkms
A: 

Both

$qry = "select * from table where id = $id";

and

$qry = "select * from table where id = " . $id;

will work and will give you the same value in $qry. Note there's no need for the ." " you had at the end of the second - all that does is append a space, which is pretty pointless.

You can also do

$qry = 'select * from table where id = ' . $id;

Which does exactly the same as the other two. They're all "correct" in that they all give you the desired result, and they all have their place. The first is quite inefficient because of the way PHP handles interpolated strings (see here for an in depth explanation), but is arguably cleaner and quicker than the other two.

Chris Smith
+2  A: 

If the string is in double quotes, variables will be evaluated. If it's in single quotes, it's literal and you'll get exactly what you type.

$bar = 42;
'Foo $bar Baz'           // Foo $bar Baz
"Foo $bar Baz"           // Foo 42 Baz
'Foo ' . $bar . ' Baz'   // Foo 42 Baz
'Foo ' . '$bar' . ' Baz' // Foo $bar Baz
"$bar " . $bar . " $bar" // 42 42 42    

Here is the relevant manual section for a full explanation:
http://php.net/manual/en/language.types.string.php#language.types.string.parsing

To put actual quotes into the string, you'll need to alternate them or escape them.

'"$bar"'    // "$bar"
"'$bar'"    // '42'
'\'$bar\''  // '$bar'
"\"$bar\""  // "42"
''$bar''    // syntax error, empty string '' + $bar + empty string ''

Also, what he said.

deceze
BTW, if you have trouble remembering that variables in double quoted stings are interpreted, imagine making double quotes in the air with your fingers. Double quoted strings may not mean what you actually type. Like, you know, this is totally "hard", riiiight? Whereby "hard" I mean 'easy'.
deceze
A: 

I am using this $qry = "SELECT * FROM table WHERE id=$id"; as I think that INT does not need quotes.

otherwise I am using $qry = "SELECT * FROM table WHERE name='$name'"; however $name need to be filtered...

pnm123
A: 

You can also try explicit variable denotation in strings like so:

$query = "SELECT * FROM table WHERE id = {$id}";

This allows you to do stuff like:

$name = "friend";
$str = "Hello {$name}s"; // Hello friends

where you couldn't do that if you tried:

$str = "Hello $names";

Since it would try to expand a variable called $names.

Variables enclosed in single quotes are not expanded and are treated as literals, so 'hey, $id' will be exactly that, instead of the 'hey, 1' expected if you used double quotes.

You can also try sprintf:

$query = sprintf("SELECT * FROM table WHERE id = %d", $id);

As the first poster said, definitely sanitize your data before queries are run.

Typeoneerror
A: 

Theres a simple way to remember this.

By using the double qoutes " your telling php that this string should be parsed for php variables.

by using the sing qoutes ' your telling php not to convert any variables into there values.

But also take not at carriages such as \r and \n by using a single qoute the carriege is not taken into consideration and it will print a literal \r or \n, but my using the double qoutes the will be converted into there actual entites such as

see what i did there :)

Hope this helps.

RobertPitt