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

Recommended Answers

All 2 Replies

time BETWEEN '$endDateTime' AND '$startDateTime' Put quotes around the dates

: ) Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.