Using {} around php variables in queries

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Using {} around php variables in queries

Post 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
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Using {} around php variables in queries

Post by Syntac »

There is no difference.

Also, remember to escape that variable or someone could easily hijack your application.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Using {} around php variables in queries

Post 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:

Code: Select all

echo "Post: $_POST['item']";
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:

Code: Select all

echo "Post: {$_POST['item']}";
-Andy
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Re: Using {} around php variables in queries

Post 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.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Using {} around php variables in queries

Post by Syntac »

andyhoneycutt: No there isn't... Unless you meant the difference between regular syntax and bracket syntax. :wink:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Using {} around php variables in queries

Post 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
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Using {} around php variables in queries

Post 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!
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Using {} around php variables in queries

Post by andyhoneycutt »

Syntac wrote:andyhoneycutt: No there isn't... Unless you meant the difference between regular syntax and bracket syntax. :wink:
lol fair enough =]
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Using {} around php variables in queries

Post 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
Post Reply