0

I am sure this is a very simple question for many people here...

$query = "SELECT count(DISTINCT gamecode) as games, sum(psminute) as minutes FROM X2015";
$res = mysql_query($query); 
while ($row =mysql_fetch_row($res)) { 
    echo "<tr><td>".$row[0]."</td><td>".$row['minutes']."</td></tr>";
}

I have no results for $row['minutes'], while I do have results when using $row[1] instead. Do you know why? Thank you very much!

Javi
  • 170
  • 3
  • 13
  • The `mysql_*` functions in PHP are deprecated and shouldn't be used. Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for information on why and what to replace them with. – Matt Raines May 16 '16 at 20:09

2 Answers2

0

Use mysql_fetch_assoc instead of mysql_fetch_row

Read this: http://us3.php.net/manual/en/function.mysql-fetch-assoc.php

$query = "SELECT count(DISTINCT gamecode) as games, sum(psminute) as minutes FROM X2015";
$res = mysql_query($query); 
while ($row =mysql_fetch_assoc($res)) { 
    echo "<tr><td>".$row['games']."</td><td>".$row['minutes']."</td></tr>";
}
Prokhor Sednev
  • 668
  • 7
  • 14
-1

When you use SUM, you also have to do a GROUP BY with every field, except that sum part

$query = "SELECT count(DISTINCT gamecode) as games, sum(psminute) as minutes FROM X2015 group by gamecode";
Carlos
  • 1,261
  • 11
  • 27