Retrieving a COUNT Value

A

Anonymous

Guest
I've searched through the MySQL manual and obviously missed the section that deals with echoing a COUNT value.

This is what I have:

$Count = "SELECT COUNT(*) FROM Logon
WHERE loginName = '{$_SESSION['logname']}'";

If I fire this into a query window it returns a value - how can i echo this value on the screen...?
 
Code:
<?php
/* Performing SQL query */
$Count = "SELECT COUNT(*) FROM Logon 
WHERE loginName = '{$_SESSION['logname']}'";
$result = mysql_query($Count) or die("Query failed : " . mysql_error());

/* Printing results in HTML */
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table>\n";
?>
Something like this?
 
Dabear

thanks for the post.

All I want to do though is echo the value. Is it really that complex...?
 
I ended up with this:

echo "\n<br><br>You have logged in a total of: ";

while ($line = mysql_fetch_array($LoginCount, MYSQL_ASSOC))
{
foreach ($line as $col_value)
{
echo "\t\t$col_value\n";
}
}

Thanks very very much for your help...!

I've been trying to sort this for two days on and off...!
 
Actually, I just copied and changed a little bit from the code sniplet on http://php.net/mysql
 
Every MySQL SELECT returns a set of rows. There might only be one row and one column, but it's still a set of rows and you must treat it as such. So in essence, Dabear is right.

If you know it's just one row you can always just call mysql_fetch_array() once and it'll work for you.

Anyway, as usual, the answers you seek are in the documenation.
 
Back
Top