Reply
Error in SQL syntax help
Old 03-08-2010, 12:37 PM Error in SQL syntax help
Average Talker

Posts: 18
Trades: 0
PHP Code:
$count=1
$result "SELECT * FROM profiles";
if(
$instrument != '') {$result .= ($count==1" WHERE"" AND") . "instrument = $instrument ";$count++;} 
if(
$purpose != '') {$result .= ($count==1" WHERE"" AND") . "purpose =$purpose"$count++;} 
if(
$seeking != '') {$result .= ($count==1" WHERE"" AND") . "seeking= '$seeking";$count++;} 
if(
$age != '') {$result .= ($count==1" WHERE"" AND") . "age = $age ";$count++;}  


$numresults=mysql_query($result); 
$num_results = mysql_num_rows($numresults);
I'm getting an error on the $num_results

"Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Users\Ash\Documents\Testing site\xampp\htdocs\upload\quick-search.php on line 118"

When I put a 'or die()' with the $numresults it gives me this 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 '= strings' at line 1"

I'm not sure why, anyone help?
applebiz89 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 03-08-2010, 12:45 PM Re: Error in SQL syntax help
NullPointer's Avatar
Will Code for Food

Posts: 1,297
Name: Matt
Location: Irvine, CA
Trades: 0
The syntax is incorrect in the query you are generating. This line:
PHP Code:
if($instrument != '') {$result .= ($count==1" WHERE"" AND") . "instrument = $instrument ";$count++;} 
will result in something like:

Code:
SELECT * FROM profiles WHEREinstrument = $instrument
Notice the lack of a space between where and instrument. You also need to quote string values in your queries. Try:

PHP Code:
$count=1;  
$result "SELECT * FROM profiles"
if(
$instrument != '') {$result .= ($count==1" WHERE"" AND") . " instrument = '$instrument' ";$count++;}  
if(
$purpose != '') {$result .= ($count==1" WHERE"" AND") . " purpose = '$purpose'"$count++;}  
if(
$seeking != '') {$result .= ($count==1" WHERE"" AND") . " seeking = '$seeking";$count++;}  
if(
$age != '') {$result .= ($count==1" WHERE"" AND") . " age = $age ";$count++;}   


$numresults=mysql_query($result); 
Also, if you continue to have problems try echo'ing your query after generating it check the syntax.
__________________
Tinsology | How to Post Code | ErrorDatabase

Last edited by NullPointer; 03-08-2010 at 12:47 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-08-2010, 12:52 PM Re: Error in SQL syntax help
Average Talker

Posts: 18
Trades: 0
Brilliant!

Thanks alot. I just couldnt work it out didnt realise there was them spaces! I guess it takes an extra pair of eyes to spot your own mistakes sometimes. That problem took me absolutely ages to do and that simple!

Thanks again
applebiz89 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Error in SQL syntax help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.10643 seconds with 13 queries