PDA

View Full Version : PHP - Resource ID to usable value?



Saldash
08-05-11, 20:35
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?

Peter
08-05-11, 21:39
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'];

Saldash
08-05-11, 22:51
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!

Peter
09-05-11, 01:03
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'];
}
}