MySQL WHERE clause not working

Discussion in 'PHP' started by Exa, Oct 4, 2008.

  1. #1
    Hey guys, thanks to all who helped me previously (enjoy your added rep.) :D

    I have this PHP code:
    
    $get_rating = mysql_query("SELECT rsite, AVG(rvalue) FROM rating GROUP BY rsite");	
    	while($ratings = mysql_fetch_array($get_rating)) {
    	print $ratings['rsite'];
    	print ': ';
    	print $ratings['AVG(rvalue)'];
    	print '<br />';
    
    }
    PHP:
    It works perfectly fine, and echoes something like:

    However, now I only want the script to echo the average of 'rsite' 3, and I add in the WHERE clause:

    $get_rating = mysql_query("SELECT rsite, AVG(rvalue) FROM rating GROUP BY rsite WHERE rsite=3");	
    	while($ratings = mysql_fetch_array($get_rating)){
    	print $ratings['rsite'];
    	print ': ';
    	print $ratings['AVG(rvalue)'];
    	print '<br />';
    
    }
    PHP:
    But the result is nothing. Just a blank space :confused:

    I also tried:

    $get_rating = mysql_query("SELECT rsite, AVG(rvalue) FROM rating WHERE rsite=3");	
    	while($ratings = mysql_fetch_array($get_rating)){
    	print $ratings['rsite'];
    	print ': ';
    	print $ratings['AVG(rvalue)'];
    	print '<br />';
    
    }
    PHP:
    ...but nothing :(

    Thanks for any help at all! :D
     
    Exa, Oct 4, 2008 IP
  2. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #2
    try this...

    
    $get_rating = mysql_query("SELECT rsite, AVG(rvalue) FROM rating WHERE rsite=3 GROUP BY rsite");    
    
    Code (markup):
     
    spc, Oct 4, 2008 IP
    Exa likes this.
  3. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your condition needs to be checked first (before grouping). So just put the WHERE clause in front of the GROUP BY statement instead of the other way around like it is now.
    (also see http://www.w3schools.com/sql/sql_groupby.asp )
     
    keyaa, Oct 4, 2008 IP
    Exa likes this.
  4. Exa

    Exa Active Member

    Messages:
    471
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Tried this, but it still doesn't work :(
     
    Exa, Oct 4, 2008 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Try adding single quotes around your value - and since you are only retrieving one record there's no reason to be declaring GROUP BY

    $get_rating = mysql_query("SELECT rsite,AVG(rvalue) FROM rating WHERE rsite='3'");

    Wait, you are using Fetcharray on a single item request. Well there's your problem. Hang on, I'll be at my workstation in a few minutes.
     
    deathshadow, Oct 4, 2008 IP
  6. spc

    spc Well-Known Member

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Use mysql_fetch_row instead of mysql_fetch_array

    
    $get_rating = mysql_query("SELECT rsite, AVG(rvalue) FROM rating WHERE rsite='3'");
    $ratings = mysql_fetch_row($get_rating);
    
        print $ratings[0];
        print ': ';
        print $ratings[1];
        print '<br />';
    
    
    PHP:
     
    spc, Oct 4, 2008 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Actually, I was just thinking it was the 'while' that could be causing the issue - I'd still use fetch_array, but with an IF statement so that if it does have an error, you can trap it thus:

    $query="SELECT rsite,AVG(rvalue)
    	FROM rating
    	WHERE rsite='3'
    ";
    $results=mysql_query($query);  
    if ($ratings=mysql_fetch_array($results)) {
    	echo $ratings['rsite'],': ',$ratings['AVG(rvalue)'],'<br />';
    } else {
    	echo 'Something went wrong in retrieving ratings.<br />';
    }
    Code (markup):
    Yes, I separated out the query just to make it easier to read. ... and since we're not returning values from printing, it's probably better to use echo.

    Try running that, if the error message comes up something's probably wrong with the query.
     
    deathshadow, Oct 4, 2008 IP