Re: array_sum on key
Posted: Mon Aug 17, 2009 5:32 pm
get_points_by_id() is a bad name for a function that doesn't return points and points alone. Your get_points_by_id() returns a lot more than that. It returns a whole array of stuff so it's not returning points. This is a source of confusion between us. I'd question the necessity to create this whole dummy structure just for the case where no rows are returned.
Things you probably already know but might not, and I'm sorry if this is getting patronising but I'm just trying to cover possibilities here:
As a matter of simplicity for yourself. This:Three lines becomes 1. Less code is better. The (int) will ensure that $play_id is converted to an integer before being concatenated to the query and is a security feature (Prevents SQL injection; Google those terms for more links).
Anyway knowing what I now do about your database schema. I think show you what you're trying to do:which can be shortened to:
which can be shortened to:
Things you probably already know but might not, and I'm sorry if this is getting patronising but I'm just trying to cover possibilities here:
- mysql_query() returns a MySQL query result resource that can be used for a variety of things including mysql_fetch_assoc() and mysql_num_rows().
- mysql_fetch_assoc() is used to retrieve the rows from a query and takes a MySQL query result resource to ascertain which query that it.
- mysql_fetch_assoc() returns a PHP array. That PHP array is that separate from MySQL and no longer has anything to do with the database. The database might change or explode but that array will stay the same. MySQL was involved in getting you that array but once you've got it MySQL can't help you anymore.
- For all intents and purposes functions beginning with mysql_ will interact with the database.
- mysql_num_rows() will tell you how many rows a query has returned by providing a result resource.
- count() will tell you how many elements are in an array.
Code: Select all
$qRes = mysql_query($q);
$rows = array();
while ($rows[] = mysql_fetch_assoc($qRes));
echo count($rows); # that's how many rows I gotCode: Select all
$qRes = mysql_query($q);
echo mysql_num_rows($qRes); # that's how many rows I'm gonna get if I called mysql_fetch_assoc($qRes) in a while loop.may be shortened toCode: Select all
$query = "SELECT * "; $query .= "FROM points "; $query .= "WHERE player_id=" . $player_id ." ";
Code: Select all
$query = 'SELECT * FROM points ' . (int)$player_id;Anyway knowing what I now do about your database schema. I think show you what you're trying to do:
Code: Select all
function getTotalPointsForPlayer($id) {
global $connection;
$qRes = mysql_query("SELECT * FROM player WHERE player_id " . (int)$id);
$rows = array();
while ($rows[] = mysql_fetch_assoc($qRes));
$points = array();
foreach ($rows as $r) { $points[] = $r['points']; }
return array_sum($points); }Code: Select all
function getTotalPointsForPlayer($id) {
global $connection;
$qRes = mysql_query("SELECT * FROM player WHERE player_id " . (int)$id);
$total = 0;
while ($row = mysql_fetch_assoc($qRes)) { $total += $row['points']; }
return $total; }Code: Select all
function getTotalPointsForPlayer($id) {
global $connection; # It may be safe to remove this line also.
$q = "SELECT sum(points) AS totalPoints FROM player WHERE player_id " . (int)$id;
return mysql_fetch_object(mysql_query($q))->totalPoints; }