Page 1 of 1
Using {} around php variables in queries
Posted: Mon Jan 05, 2009 4:19 pm
by mikebr
I have been starting to notice MySQL queries that now have the variables wrapped in {}, can't seem to find an explanation for this, maybe someone could explain the difference between the following two queries:
Code: Select all
<?php
mysql_query(“SELECT * FROM users WHERE name=’$name’”);
?>
Code: Select all
<?php
mysql_query(“SELECT * FROM users WHERE name=’{$name}’”);
?>
Thanks in advance
Re: Using {} around php variables in queries
Posted: Mon Jan 05, 2009 4:20 pm
by Syntac
There is no difference.
Also, remember to escape that variable or someone could easily hijack your application.
Re: Using {} around php variables in queries
Posted: Mon Jan 05, 2009 4:36 pm
by andyhoneycutt
Actually, there is a difference, but as far as I know, only for complex data types like arrays and classes. For strings and numerical types it should work fine. The first example fails:
Will result in this lovely error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
This is accepted, however:
-Andy
Re: Using {} around php variables in queries
Posted: Mon Jan 05, 2009 5:26 pm
by mikebr
So what you are saying is it only is of use if enclosed in double quotes and the following then wouldn't need it?
Code: Select all
<?php
echo "Post: " . $_POST['item'];
// or
mysql_query("SELECT * FROM users WHERE name=’" . $name . "’");
?>
Thanks
BTW, understand about the escaping.
Re: Using {} around php variables in queries
Posted: Mon Jan 05, 2009 7:24 pm
by Syntac
andyhoneycutt: No there isn't... Unless you meant the difference between regular syntax and bracket syntax.

Re: Using {} around php variables in queries
Posted: Tue Jan 06, 2009 1:35 am
by josh
Code: Select all
$var = 'test';
echo "This is a $variing"; // does not work
echo "This is a {$var}ing"; // does not work, outputs "this is a testing"
You should use string concatenation instead IMO, its more consistent and understandable to more programmers, plus syntax highlighting if you don't have Zend
Re: Using {} around php variables in queries
Posted: Tue Jan 06, 2009 1:39 am
by omniuni
I agree about using concatenation, but that's a cool trick for when you need to put together a bunch of stuff and have PHP correctly find variables of Array type and such.
Thanks All!
Re: Using {} around php variables in queries
Posted: Tue Jan 06, 2009 9:31 am
by andyhoneycutt
Syntac wrote:andyhoneycutt: No there isn't... Unless you meant the difference between regular syntax and bracket syntax.

lol fair enough =]
Re: Using {} around php variables in queries
Posted: Tue Jan 06, 2009 9:32 am
by andyhoneycutt
omniuni wrote:I agree about using concatenation, but that's a cool trick for when you need to put together a bunch of stuff and have PHP correctly find variables of Array type and such.
Thanks All!
I also agree with concatenation being a more preferred method. It's easier to decipher.
-Andy