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'];
}
-
2Am I missing something or is there really no difference in your queries?Felix Kling– Felix Kling2010年03月30日 22:11:31 +00:00Commented Mar 30, 2010 at 22:11
-
1In all three cases $query contains the same string ...is that intentional?VolkerK– VolkerK2010年03月30日 22:11:42 +00:00Commented Mar 30, 2010 at 22:11
-
What are the values of $day1, $day2 and $service? Can you echo them?Mark Byers– Mark Byers2010年03月30日 22:13:25 +00:00Commented Mar 30, 2010 at 22:13
-
what are the values of $day1 and $day2 , they should be like ex '2010年03月31日' ... if not your query-result will be empty... also, the trailing ; in your query is not needed...Vidar Vestnes– Vidar Vestnes2010年03月30日 22:14:18 +00:00Commented Mar 30, 2010 at 22:14
-
2@hypnocode: Actually with ZERO, do you mean 0 (number zero) or NULL ? Or maybe even an empty string?Felix Kling– Felix Kling2010年03月30日 22:17:19 +00:00Commented Mar 30, 2010 at 22:17
6 Answers 6
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'];
}
5 Comments
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.
5 Comments
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);. Then an exception is thrown each time something goes wrong. Not everybody likes exceptions, but imo at least it's a bit harder to "ignore" them or to forget the error handling.Your queries are not formatter properly they are :
"SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59';";
They should be :
"SELECT COUNT(*) as NUM FROM `items` WHERE `modified` BETWEEN '$day1 00:00:00' AND '$day2 23:59:59'";
You have an extra semi-colon in each query. and use or die mysql_error() to print out the error.
Also this part :
$num = mysql_fetch_array ($result);
return $num['NUM'];
I'd replaced with :
$num = mysql_fetch_array ($result);
extract($num);
return $NUM;//if this is your field name
8 Comments
; is the query deliminator.Fixed it...dumb mistake. I'm so new to PHP and MySQL, sorry.
getCountBetweenDays(2010年3月1日,2010年3月28日,CONT_ALL_SERVICE);
should have been:
getTweetCountBetweenDays('2010-3-1','2010-3-28',CONT_ALL_SERVICE);
Thanks all for the help and I now know how to debug properly!
Comments
You're trying to reference a numeric index [n] with an associative index ['s']. Either specify this to your query
mysql_fetch_array($result, MYSQL_BOTH)
Or just do
mysql_fetch_assoc($result);
Which will let you reference indices by association
Comments
return $num['NUM'];
means return the 'num' part of the array; Change 'NUM' to 1 or 2 and then try
3 Comments
mysql_fetch_array() returns an array with both column numbers AND column names. mysql_fetch_assoc() returns only an array with column names.mysql_fetch_array fetches both the numeric and the associative part by default.mysql_fetch_array returns the result as numerical and associative array. So the array access should work imo.