jason360 Posted December 7, 2013 Share Posted December 7, 2013 (edited) Hey guys, I have a mysql query inside 2 if statements. When the query is just inside the first if statement there is no problem, but once I add the query inside the second if statement as well I get an 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 '5''' at line 1 I need the $vote from the query to be in both if statements. I tried listing the query before both if statements but I get the same issue. I am not too sure what I am doing wrong here. Any help much appreciated! Thank you. JK Here is the problem area of my code: if($d['user_id'] == $_SESSION['user_id']){ $result = mysql_query( "SELECT member_vote FROM member_rank WHERE member_id='".$d['user_id']."'" ); list($vote) = mysql_fetch_array( $result ) or die($myQuery."<br/><br/>".mysql_error()); return ' <div class="vote-span"><!-- voting--> <div class="vote-score">'.$vote.'</div> </div> ';} if($d['user_id'] != $_SESSION['user_id']){ $result = mysql_query( "SELECT member_vote FROM member_rank WHERE member_id='".$d['user_id']."'" ); list($vote) = mysql_fetch_array( $result ) or die($myQuery."<br/><br/>".mysql_error()); return ' <div class="vote-span"><!-- voting--> <div class="vote-score">'.$vote.'</div> </div> ';} Edited December 7, 2013 by jason360 Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted December 7, 2013 Solution Share Posted December 7, 2013 (edited) your $d['user_id'] value probably contains something that is breaking the sql syntax. you need to ALWAYS form your sql query statement in a php variable so that you can echo/log what your query is for debugging purposes. also, your or die(...) statement on the end of the mysql_fetch_array( ) statement means that your code will die when the query doesn't match any rows. you need the or die () statement on the end of the msyql_ query() statement so that it will only trigger the die statement when the query fails due to an error and in fact the error you are getting in this case may be from a previous query statement and that the only thing wrong with the code posted in this thread is that it isn't matching any row in your database table. fix the code so that the or die(...) statements are on the mysql_query() statements and report back of you are still getting any errors from the posted code. lastly, the posted code inside each conditional statement looks identical to me. there's no point in repeating the same query and same result logic just because you have different input values. Edited December 7, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
jason360 Posted December 7, 2013 Author Share Posted December 7, 2013 mac_gyver Thanks for your help. I was able to figure out my problem from your suggestions. I had to change the: WHERE member_id='".$d['user_id']."' To this: WHERE member_id=".$d['user_id']." The extra set of '' was messing it up. Regarding the identical code, I just posted my example up wrong. The other if statement doesn't have the $vote Thanks again! JK Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 7, 2013 Share Posted December 7, 2013 the change you made indicates that $d['user_id'] contains some single-quotes that are part of the sql syntax. it is generally best if data is the only thing in variables and any syntax that is part of the query is only in the query statement. i'm guessing you are using DreamWeaver produced code? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.