What is the most efficient way to do this?
Right now I am doing it this way..
$m= date("m");
$de= date("d");
$y= date("Y");
$userid = $_SESSION['user_id'];
//today
$date = date("Y-m-d");
$today = mysql_query("SELECT *
FROM cdr
WHERE accountcode = '$userid'
AND calldate LIKE '$date%'") or die(mysql_error());
$counttoday = mysql_num_rows($today);
//yesterday
$yesterdaydate = date('m/d/Y', mktime(0,0,0,$m,($de-1),$y));
$yesterday = mysql_query("SELECT *
FROM cdr
WHERE accountcode = '$userid'
AND calldate LIKE '$yesterdaydate%'") or die(mysql_error());
$countyesterday = mysql_num_rows($yesterday);
then
print $counttoday;
print $countyesterday;
I know there has to be a better way to do this.
Let me know, thanks!
I don’t know if this is an absolute better way to do what you’re trying to do, but you can use the MySQL statement COUNT() to count the number of rows returned by a query.
So for example, writing
Will return a 1×1 table of results with the number of rows in the query.
Then you can use
to fetch the count.
There doesn’t seem to be a faster way to do this with one function call, unless you define your own function I suppose. But for a very large result from your initial query, I would imagine this is faster since I assume mysql_num_rows() has to count each row individually all over again. Doing it this way only goes over the rows of the query result once, when the result itself is being populated.