tags:

views:

61

answers:

4

This is what I got, UPDATED

$avg_query = "SELECT AVG(rating) AS avg_rating FROM kicks WHERE userid='$userid'";
                        $avg_result = mysqli_query($cxn, $avg_query) or die("Couldn't execute query.");

                        $row = mysqli_fetch_assoc($avg_result);
                        $average = $row['avg_rating'];

Every time I echo it out, it always returns array. Never used this function before, but i don't understand why it would do that.

+5  A: 

Shouldn't you be assigning to $row here?

$row = mysqli_fetch_assoc($avg_result);
$average = $row['AVG(rating)'];

I'd also suggest using an alias when you are selecting the result of a function call:

$avg_query = "SELECT AVG(rating) AS avg_rating FROM kicks WHERE userid='$userid'"
...
$average = $row['avg_rating'];

If this still doesn't give any result it is probably because:

  • There are no rows in that table for that user.
  • There is a row in that table where the rating is set to the value NULL.

In the second case you can fix it by filtering out those rows:

SELECT AVG(rating) AS avg_rating
FROM kicks
WHERE userid='$userid'
AND rating IS NOT NULL
Mark Byers
Good Idea, but nope, i still get "Array".
Rick Bross
Just place the var_dump or print_r.It takes 10 seconds to do that. You wasted 10 minutes of your time and of other people's time on this trivial question.
Alexander
@Alexander: If you knew the answer why didn't you just post it?
Mark Byers
@Mark, I don't. If I had telepathic abilities, I probably would. This is why I ask for some var_dump here, to know what is really happening, because there is too little information.
Alexander
If I var_dump($average); I get NULL back.
Rick Bross
var_dump($row).
Alexander
array(1) { ["avg_rating"]=> string(6) "4.0000" }
Rick Bross
well then, when you write $row['avg_rating'], you should get 4.0000. This way arrays should work in PHP. Nothing to do with MySQL or anything else. That is working, according to the var_dump. You either have a typo, or messed up a bit with references somewhere.
Alexander
Somehow, it works now....
Rick Bross
+2  A: 
$avg = mysqli_fetch_assoc($avg_result);
$average = $row['AVG(rating)'];

where did you get $row?

bobrik
+1 Good Question!
jigfox
@bobrik: +1 I just noticed this too and updated my answer, but you were fastest.
Mark Byers
you right, i should be this:$row = mysqli_fetch_assoc($avg_result); $average = $row['avg_rating'];but with that nothign shows up, not even "Array"
Rick Bross
@Rick Bross: What is the query that you are running now? Is it the same as in the question?
Mark Byers
ill update the post.
Rick Bross
A: 

Btw, this should be done something like this:

$avg_query = "SELECT AVG(id) as average_id FROM unfollowr_users";
$avg_result = mysql_query($avg_query) or die("Couldn't execute query.");

$row = mysql_fetch_assoc($avg_result);
$average = $row['average_id'];
echo "$average\n";

AVG is not an PHP function it's SQL function. We just add better name when we SELECT.

bobrik
1. Im using mysqli, not mysql and for some reason, that isnt returning anything at all, even in mysqli.
Rick Bross
+1  A: 

The string "Array" is what you get when PHP thinks you want it to convert an array to a string. If you see the word "Array" where you expected more meaningful output, it means that you tried to treat a variable containing an array as though it actually contained a string.

When you see this behaviour from an echo command, one thing you could try is to place the following before the echo, as a debugging measure (let's say your variable's name is $var):

echo '<pre>';
var_dump($var);
echo '</pre>';

You will then see something like

array(0) {
0 => string(3) 'cat'
1 => string(6) 'doggie'
}

This tells you what structure your array has and what its keys are. You should then look back in your code and try to work out why your variable contains an array when you had expected a string - perhaps you had misunderstood the behaviour of a standard library function, or something like that.

Hammerite