1

So I have a query that returns the following results in PHP, and I'm trying to add the total of the hours_played, I have tried array_sum() but that does not seem to work, I would like to be able to add the values of hours_played to be 11 that way I can then pass that it another function :

Array
(
   [id] => 1
   [team_id] => 1
   [hours_played] => 1
)
Array
(
   [id] => 2
   [team_id] => 1
   [hours_played] => 4
)
Array
(
   [id] => 3
   [team_id] => 1
   [hours_played] => 6 
)

I'm a little bit lost and do I need to store the able Arrays in another array to be able to do this? The results above are from a sql query and is what is printed on the page when I print_r the $row variable.

$sql = "SELECT * FROM table1 WHERE team_id = 1";
$res = mysql_query($sql);

while($row = mysql_fetch_assoc($res)){
   print_r($row);
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
ImANobody
  • 29
  • 4

4 Answers4

3

You can either sum all the columns hours_played after having fetched the results and assigned them into an array, as shown below

$sql = "SELECT * FROM table1 WHERE team_id = 1";
$res = mysql_query($sql);
$result = array();
while($row = mysql_fetch_assoc($res)){
   $result[] = $row;
}

$sum = array_sum(array_column($result, "hours_played"));
echo $sum;

..or you can sum it up as you loop the results.

$sql = "SELECT * FROM table1 WHERE team_id = 1";
$res = mysql_query($sql);
$sum = 0;
while($row = mysql_fetch_assoc($res)){
    $sum += $row['hours_played'];
}

echo $sum;

Or, if you just need the total hours played, you can do that directly in one query, using COUNT() in MySQL.

$sql = "SELECT SUM(hours_played) as sum_hours_played FROM table1 WHERE team_id = 1";
$res = mysql_query($sql);

$row = mysql_fetch_assoc($res);
echo $row['sum_hours_played'];

WARNING
Do not use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article can help you decide which.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • You have the parameters to `array_column` in the wrong order – Nick Feb 26 '19 at 21:56
  • That's what you get for writing code without testing it just before hitting the haysack. Thanks, I'll fix that. – Qirel Feb 27 '19 at 06:13
1

You're really close . You just need to add the 3rd index of each array together:

$sql = "SELECT * FROM table1 WHERE team_id = 1";
$res = mysql_query($sql);

$total = 0;
while($row = mysql_fetch_assoc($res)){
   $total += $row['hours_played'];
}

CONVERSELY You can use the numbered index as well.

$total = 0;
while($row = mysql_fetch_assoc($res)){
   $total += $row[2]; // indexes start at 0
}

echo $total; // Will print 11 if your arrays you posted are correct.

IN ADDITION --- WARNING!! You need to be using mysqli instead of the far less secure and now deprecated mysql

Zak
  • 6,976
  • 2
  • 26
  • 48
1

Most Important
Avoid using mysql_* functions. They are no longer maintained and are also officially deprecated. Read about using prepared statements instead, and change your existing code to use PDO or MySQLi

You can get a total of hours_played in the while loop. Try this:

$sql = "SELECT * FROM table1 WHERE team_id = 1";
$res = mysql_query($sql);

$total_hours_played = 0; // initialize a variable to get sum of hours
while($row = mysql_fetch_assoc($res)){
   $total_hours_played += $row['hours_played'];
}
echo $total_hours_played;
Satish Saini
  • 2,880
  • 3
  • 24
  • 38
0
$sum = array_sum(array_column($array, 'hours_played'));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Markownikow
  • 457
  • 1
  • 4
  • 14