View Full Version : PHP - Resource ID to usable value?
Hey guys,
Kinda shocked I can't remember this but.. I'm writing a short and simple query to add all the rows from
a single column together.. (then work with that value later)
Thus far from what I understand, this should work;
$total_points = mysql_query("SELECT SUM(points) FROM user_table WHERE 1") or die(mysql_error());
But it gives me a Resource ID and not the integer value I was hoping for :picard:
Does anyone have a solution for this please?
Hey guys,
Kinda shocked I can't remember this but.. I'm writing a short and simple query to add all the rows from
a single column together.. (then work with that value later)
Thus far from what I understand, this should work;
$total_points = mysql_query("SELECT SUM(points) FROM user_table WHERE 1") or die(mysql_error());But it gives me a Resource ID and not the integer value I was hoping for :picard:
Does anyone have a solution for this please?
No need for the WHERE clause either:
$total_points = mysql_query("SELECT SUM(points) AS point_sum FROM user_table") or die(mysql_error());
$points_data = mysql_fetch_assoc($total_points);
echo $points_data['point_sum'];
No need for the WHERE clause either:
$total_points = mysql_query("SELECT SUM(points) AS point_sum FROM user_table") or die(mysql_error());
$points_data = mysql_fetch_assoc($total_points);
echo $points_data['point_sum'];
oh sweet, completely different from what i was considering doing, and works a treat!
Thanks you!
oh sweet, completely different from what i was considering doing, and works a treat!
Thanks you!
If you want to retrieve multiple rows in the future:
$my_query = mysql_query("SELECT * FROM user_table") or die(mysql_error());
if (mysql_num_rows($my_query) > 0) {
while ($data = mysql_fetch_assoc($my_query)) {
// Handle your data here
echo $data['points'];
}
}
Powered by vBulletin® Version 4.1.12 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.