You don't need a "while" statement as you're returning only 1 row. You should always check to make sure that $result is populated before reading a value from it.
$result = mysql_query($sql); if($result) { $row = mysql_fetch_row($result); $count = $row[0]; echo$count; } else { echo"no result from database."; }
There is another way you can do this. Here is how I would do it, it doesnt use MySQL to do the count, instead it uses PHP.
$sql = "SELECT * FROM table"; $result = mysql_query($sql, $connection); if($result) printmysql_num_rows($result); else print"There was no result returned";
You can do just:
$row = mysql_fetch_array($result);
$count = $row['count'];
You should always check to make sure that $result is populated before reading a value from it.
$result = mysql_query($sql);
if($result)
{
$row = mysql_fetch_row($result);
$count = $row[0];
echo $count;
}
else
{
echo "no result from database.";
}
and
u must use a field name in COUNT() function for performance. like:
SELECT COUNT(this) FROM my_table WHERE this='$this'
http://www.morad.info/
$sql = "SELECT * FROM table";
$result = mysql_query($sql, $connection);
if ($result)
print mysql_num_rows($result);
else
print "There was no result returned";