Jump to content

Mysql query within if statement problem.


jason360
Go to solution Solved by mac_gyver,

Recommended Posts

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 by jason360
Link to comment
Share on other sites

  • Solution

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 by mac_gyver
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.