Im trying to get a count on how many rows i have in a table called Articles, at the momement there are no entries so i need the count function to return zero, however when i echo the results, it is blank.
Here is my code for the mysql query:
mysql_select_db("Articles", $con);
$countR = mysql_query('SELECT COUNT(*) FROM Articles');
mysql_close($con);
Thanks everyone,
Maxdream
Your first point of action when an echo does something unexpected is to use var_dump. You’ll probably find that, if you’re trying to echo $countR, it will say that the variable is actually a resource. Because it is.
You need to get the result from the query before using it. The query returns a resultset just like any other query, but with only 1 row and 1 field.
$countQuery = mysql_query('SELECT COUNT(*) FROM Articles');
$count = mysql_result($countQuery, 0);
Thanks, i understand what your saying, that you need to optimize the result of the query before you use it, i tried your code and i ended up with and error though:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource,
i take this to mean that its simply outputting a number rather than a MySQL type result, yet if that was true my orevious method would have worked…
I think im confused with what exactly the COUNT function outputs and how to handle it.
Right, but how can i access the integer, so that i can echo it, because i couldn’t just save the query as a function and echo that…
thanks everyone but ifixed the problem i ended up outputting it like this:
$query = "SELECT id, COUNT(id) FROM Articles";
$result = mysql_query($query) or die(mysql_error());
while($count = mysql_fetch_array($result)){
echo $count['COUNT(id)'];
}
bit the real problem was that i misspelled my selected database, oops:rolleyes: