SQL COUNT and phpMyAdmin

Hello,

I’m having a problem getting the exact result from a query I am running which involves the COUNT function. For some reason the query does not count the first row in my database. It doesn’t work correctly from the page that I am running it on but when I run the same query in phpMyAdmin it works fine and displays the correct result. Does anyone know what might be causing this? Here is the code below:

[B]$sql4 = “SELECT a, COUNT(b) AS count FROM table GROUP BY a ASC”;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

<?
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo “$row[a]) $row4[count]”;
}
?>[/B]

If anyone can help me with this problem I am having, that would be awesome. Thanks in advance! :smiley:

hi there…

try this


$sql4 = "SELECT a, COUNT(b) AS count FROM table GROUP BY a ASC";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

<?
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// echo "$row[a]) $row4[count]";
// change $row4[count] to $row[count];
echo "$row[a]) $row[count]";
}
?>

let us know how you fair out :slight_smile:

regards,

mel’

Sorry, the row4 was a mistake. I forgot to change that when I simplified the script and posted it to the forums. Here is the code again:

$sql = “SELECT a, COUNT(b) AS count FROM table GROUP BY a ASC”;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

<?
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo “$row[a]) $row[count]”;
}
?>

Anyone know why this wouldn’t count the first row in my table. I tried it in phpMyAdmin and the query worked but it doesn’t work on the page that I have it on???

It’s because you called mysql_fetch_arry once already, which fetched the first result and moved the mysql pointer to the second result.

Scratch that first reference to mysql_fetch_array.

duh… i should have noticed that one. thanks for your help! it works now.