tags:

views:

48

answers:

4
$query = mysql_query("SELECT sum(rating) FROM stars WHERE post_id = 20");
echo $query;

It outputs:

Resource id #3

instead of 8, which is what the actual sum is.

+4  A: 

Try the below. mysql_query returns a 'resource' that represents the resultset, and to get values you need to use one of the mysql_fetch_ functions.

$row = mysql_fetch_array($query);
echo $row[0];
Will A
Causes the following error: Parse error: syntax error, unexpected '[', expecting ',' or ';' in C:\xampp\htdocs\ratings.php on line 36
Fat Toney
That's more pythonish than phpish.
Ben
Edited so that it *should* work...
Will A
+1  A: 

$query, after executing the query doesn't have just a number. It'll have a Resource just like any other query you would execute.

In order to actually get the result, treat it like you would treat any other resource:

$result = mysql_fetch_array($query);
echo $result[0];
Ben
A: 
$result = mysql_fetch_array($query);
echo $result[0];
Eton B.
A: 

As mentioned before, this is because the variable is a resource. Visit this link to learn more about PHP variable types.

It is only possible to echo strings or types that can be cast to strings. Such as int, float and objects implementing the __toString magic function.

jpluijmers