db queries & single quotes?

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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

db queries & single quotes?

Post by nigma »

Hey, when you are doing a mysql_query() and such which of the following examples is better:

їcode]mysql_query("select name from names");ї/code]
їcode]mysql_query("select name from 'names'");ї/code]
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

Code: Select all

mysql_query("select name from 'names'");
i think this is good makes things clear
jollyjumper
Forum Contributor
Posts: 107
Joined: Sat Jan 25, 2003 11:03 am

Post by jollyjumper »

Code: Select all

select * from 'tablename'
throws an error at my mysql database, but

Code: Select all

select * from tablename
does work, so I would say that the first option is the only option.

Greetz Jolly.
DaMastaBugz
Forum Newbie
Posts: 2
Joined: Tue Aug 19, 2003 9:21 am
Location: Sao Paulo

Post by DaMastaBugz »

I think this is kind of a personal issue.
I know people who prefer to use single quotes and people who just hate that. I fall into the second group.
In cases where you name your tables "my table" (not the space in the name) the single quotes are a must, but if you are a reasonable bloke, you'd never name your tables with special characters and spaces to start off.

Hope this helps
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I am not aware of that using single or double quotes make a difference in MySQL.
However they make a huge, but very subtle difference in PHP. Everything between double-quotes will be get parsed, i.e. searched for variables which will get replaced with their value.
Everything between single-quotes is taken as text.

Example:

Code: Select all

$var='cookie';
echo "my $var is different";
echo 'my $var is different';
Depending on how much text there is between the double-quotes, this parsing slows the output down. Hence, most people use single quotes.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Thanks a bunch guys.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

yup
and i single quote all php strings, and keep the double quotes for insides strings: sql 'output' and html
its easy to remember when you have a specific system you use (like i always use single quotes and concat to put in a variable)
Much less confusion when you try to read it later on :)
Post Reply