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; }