Javi Javi - 6 months ago 9
PHP Question

Simple select, but reading the result through column name

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!

Answer

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