Page 1 of 1

COUNT vs mysql_num_rows

Posted: Fri Feb 06, 2004 1:24 am
by llanitedave
What I want to do is:

Code: Select all

<?php
$sql = "select count from myTable where myField > yourDate";
$result = mysql_query($sql);
?>
But I'm not sure how to reference the result.
If I change the query to:

Code: Select all

<?php
$sql = "select * from myTable where myField > yourDate";

?>
In that case I can use mysql_num_rows to get the equivalent of "Count".
But is that how I really want to do it? It seems that there should be a more direct way to get Count out of the database.

Posted: Fri Feb 06, 2004 2:47 am
by twigletmac
Try this:

Code: Select all

$sql = "SELECT COUNT(myField) FROM myTable WHERE myField > yourDate";
$result = mysql_query($sql);
$count = mysql_result($result, 0);
For just getting one result from the database [php_man]mysql_result[/php_man]() is quite handy.

Mac

Posted: Fri Feb 06, 2004 12:32 pm
by llanitedave
Thanks! The PHP manual put me off a bit from trying this when it mentioned that mysql_result is slower than using a command that takes entire rows at once. Maybe I misinterpreted the meaning of it, if I'm applying it to just a single piece of data.