Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with PHP Variable and MySQL WHERE clause

Status
Not open for further replies.

BradSour

Programmer
Jan 18, 2005
3
US
I've tried everything!

This section of code doesn't work - Query failed: Unknown column 'Acura' in 'where clause'

$make = "Acura";
$query = "SELECT DISTINCT model FROM $table WHERE make = $make";

This section will work just fine.
$query = "SELECT DISTINCT model FROM $table WHERE make = 'Acura'";

Anyone know what is going wrong?
 
You need single quotes around $make
Code:
$make = "Acura";
$query = "SELECT DISTINCT model FROM $table WHERE make = '$make'";

Ken
 
I tried that and then the query didn't return anything. I'll try it again.
 
Here's a more extensive explanation:
A SQL query command is a string which is passed to the MySQL server. All string values in the query need to be quoted in order to be recognized - otherwise the server thinks you are refering to a column name, function name etc.
Ok, the server doesn't think, it just follows rules.
 
I like to keep my php variables separate from strings... What Ken said should work as well, but for my own part I would type it like this:

Code:
$make = "Acura";
$query = "SELECT DISTINCT model FROM ".$table." WHERE make = '".$make."'";
echo($query);

That last line is just for troubleshooting purposes to make sure that the query is being structured properly before it gets passed to mysql. Even if you don't write it as I have above, this would be my first troubleshooting step - see what the query actually is that's being sent. If the query looks OK, I'd check the table to make sure there is actually something in there with Acura in the make field.

Marc
 
Thanks for your help guys. I actually had to do something totally different with $_GET[]. But I appreciate your efforts. What Ken wrote did work too. But not for what I needed to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top