I'm having trouble with this PHP function. It keeps returning zero, but I know the SQL statement works because I've queried it myself. Any ideas what I'm doing wrong? The last line makes no sense to me...I'm editing this code that someone else wrote. This function should return a number in the hundreds, assuming the date is in March. Thanks!
function getCountBetweenDays($day1,$day2,$service)
{
global $conn;
if ($service==1){
$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";}
elseif($service==2){
$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";}
elseif($service==3){
$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";}
$result = mysql_query($query,$conn);
$num = mysql_fetch_array ($result);
return $num['NUM'];
}
Try some debug output in your function, e.g.
function getCountBetweenDays($day1,$day2,$service)
{
global $conn;
switch($service) {
case 1:
$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'";
break;
case 2:
$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'";
break;
case 3:
$query = "SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'";
break;
default:
die('unknown value for $service');
}
echo '<pre>Debug: $query=', htmlspecialchars($query), '</pre>';
$result = mysql_query($query,$conn) or die('mysql_query failed: '.htmlspecialchars(mysql_error($conn)));
echo '<pre>Debug: numrows=', mysql_num_rows($result), '</pre>';
$num = mysql_fetch_array($result);
return $num['NUM'];
}
For starters, add
if (!$result) echo mysql_error();
after each mysql_query() call to see whether there are any errors. I'm pretty sure there are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With