Solved: MySQL Help

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

lumentec

Thread Starter
Joined
Mar 23, 2008
Messages
103
In PHP, should I use
Code:
mysql_query("SELECT something FROM table WHERE something=$something")
or should I add a ' before and after the variable I'm comparing like this:
Code:
mysql_query("SELECT something FROM table WHERE something='$something'")
?
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
If it is a string use the latter, otherwise use the former.

Also, unless you know what $something is you should escape it.
 

lumentec

Thread Starter
Joined
Mar 23, 2008
Messages
103
Actually... you were wrong. Completely. And it took me about 2 hours to figure out how to fix it. If you're going to use a variable inside a MySQL command, it has to be surrounded in ' ' or it won't work properly.
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
Actually... you were wrong. Completely. And it took me about 2 hours to figure out how to fix it. If you're going to use a variable inside a MySQL command, it has to be surrounded in ' ' or it won't work properly.
Can you show me an example of what you mean?
 

lumentec

Thread Starter
Joined
Mar 23, 2008
Messages
103
Code:
$result = mysql_query("SELECT * FROM user WHERE username='$username'");
as opposed to:

Code:
$result = mysql_query("SELECT * FROM user WHERE username=$username");
 

lumentec

Thread Starter
Joined
Mar 23, 2008
Messages
103
I was comparing it to a variable.
You said to use no ' 's if it wasn't a string. A variable is not a string so I didn't use ' 's. That made sense to me too because Javascript and PHP interperate anything inside " " or ' ' to be a string. But, that's not the case.

No matter if it's a variable or not, it still needs ' 's.
 

MMJ

Guest
Joined
Oct 15, 2006
Messages
3,625
I was comparing it to a variable.
You said to use no ' 's if it wasn't a string. A variable is not a string so I didn't use ' 's. That made sense to me too because Javascript and PHP intemperate anything inside " " or ' ' to be a string. But, that's not the case.

No matter if it's a variable or not, it still needs ' 's.
I'm sorry but that is wrong.

Only strings need to be quoted.
 

lumentec

Thread Starter
Joined
Mar 23, 2008
Messages
103
No. You're wrong. In a mysql_query() you have to surround variables with ' 's or you'll get an error.
 

lumentec

Thread Starter
Joined
Mar 23, 2008
Messages
103
I got it from my page which works on that principal. I guess it's possible we're using different versions of PHP... but for me, the only way it will work is if I use ' 's.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top