I have the following code which results in a mysql error. However, when I use this query in PHPMyAdmin it works fine. Can anyone see what is causing the error?

for($i=0; $i<10; $i++)
		$nextDay = $i+1;
	  	$startDateTime = date("Y-m-d H:i:s", strtotime("-$i days"));
  		$endDateTime = date("Y-m-d H:i:s", strtotime("-$nextDay days"));
  		$query = "select title, app_id, count(*) as cnt from app_instances where (time between $endDateTime and $startDateTime) and (app_id = $subjectId)  group by title";
  		$result = mysql_query($query) or die("error: ".mysql_error());
  		$title = $row['title'];
		$count = $row['cnt'];
		$appId = $row['app_id'];
		echo $startDateTime."  ";
		echo $title."  ";
		echo $count."  ";
		echo $endDateTime;
		echo '<br />';

Creates the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '16:02:51 and 2009-11-17 16:02:51) and (app_id = 2) group by title' at line 1
7 Years
Discussion Span
Last Post by benkyma
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.