Another SQL syntax error!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Another SQL syntax error!

Post by cap2cap10 »

Greetings php technorati. Ok, I know this is probably a simple syntax error, but please
enlighten me. Here is the code:

Code: Select all

[color=#4000BF]$query = "SELECT * FROM js_profile WHERE category ='$category', years_exp = '$years_exp',
    degree ='$degree', work_time ='$work_time', available ='$available' LIMIT 0, 2000 ";
    $result = mysql_query($query) or die(mysql_error());[/color]
Here is the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' years_exp = '', degree ='', work_time ='', available ='' LIMIT 0, 2000' at line 1

:banghead: tired of staring at variables. Please advise!!

Thanks in advance!

Batoe
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Another SQL syntax error!

Post by alex.barylski »

Just a guess but if $work_time is an integer there is no need to wrap in single quotes.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Another SQL syntax error!

Post by jayshields »

Use AND instead of commas for seperating up your WHERE clause.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Another SQL syntax error!

Post by susrisha »

Code: Select all

 
$query = "SELECT * FROM js_profile WHERE category ='$category' AND years_exp = '$years_exp' AND
    degree ='$degree' AND work_time ='$work_time' AND available ='$available' LIMIT 0, 2000 ";
 
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Another SQL syntax error!

Post by Apollo »

jayshields wrote:Use AND instead of commas for seperating up your WHERE clause.
or OR ;)
cap2cap10 wrote: Here is the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' years_exp = '', degree ='', work_time ='', available ='' LIMIT 0, 2000' at line 1
Since SQL reports the query as years_exp='' and degree='' etc, are you sure that $years_exp and $degree etc actually contain values? (the query itself would still be valid (besides the commas) with empty strings, but it's probably not what you intended)
Post Reply