Page 1 of 1

PHP Tutorial: Blog (Including Commenting) [part 03] #Error#

Posted: Tue Dec 06, 2011 9:44 pm
by Noso1066
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 '`Posts`.`postUser` AS `User`, DATE_FORMAT(`Posts`.`postDate`, '%d/%m/%y %H:%' at line 5

Can anyone help?
function getPosts(){
	$sql = "SELECT
				`Posts`.`postID` AS `ID`,
				`Posts`.`postTitle` AS `Title`,
				LEFT(`Posts`.`postBody`, 512) AS `Preview`
				`Posts`.`postUser` AS `User`,
				DATE_FORMAT(`Posts`.`postDate`, '%d/%m/%y %H:%i:%s') AS `Date`,
				`Comments`.`totalComments`,
				DATE FORMAT(`Comments`.`lastComment`, '%d/%m/%y %H:%i:%s') AS `lastComment`
			FROM `Posts`
			LEFT JOIN (
				SELECT 
					`postID`,
					COUNT(`commentID`) AS `totalComments`,
					MAX(`commentDATE`) AS `lastComment`
				FROM `Comments`
				GROUP BY `postID`
			) AS `Comments`
			ON `Posts`.`postID` = `Comments`.`postID`
			ORDER BY `Posts`.`postDate` DESC";
	
	$posts = mysql_query($sql)or die(mysql_error());
	
	$rows = array();
	while (($row = mysql_fetch_assoc($posts)) !== false){
		$rows[] = array(
			'ID'			=> $row['ID'],
			'Title'			=> $row['Title'],
			'Preview'		=> $row['Preview'],
			'User'			=> $row['User'],
			'Date'			=> $row['Date'],
			'totalComments'	=>($row['totalComments'] === null) ? 0 : $row['totalComments'],
			'lastComment'	=>($row['lastComment'] === null) ? 'never' : $row['lastComment']
		);
	}	
		return $rows;
}

Re: PHP Tutorial: Blog (Including Commenting) [part 03] #Er

Posted: Tue Dec 06, 2011 9:55 pm
by Jaami
Line 5, you forgot a ","
 LEFT(`Posts`.`postBody`, 512) AS `Preview`,

Re: PHP Tutorial: Blog (Including Commenting) [part 03] #Er

Posted: Tue Dec 06, 2011 10:42 pm
by Noso1066
That's great thanks - worked as stated although now I have a similar message about "line 8" but I cannot seem to find the syntax error, any thoughts?

Re: PHP Tutorial: Blog (Including Commenting) [part 03] #Er

Posted: Tue Dec 06, 2011 10:59 pm
by Jaami
Line 9, you forgot a "_" in "DATE FORMAT".
DATE_FORMAT(`Comments`.`lastComment`, '%d/%m/%y %H:%i:%s') AS 

Re: PHP Tutorial: Blog (Including Commenting) [part 03] #Er

Posted: Fri Dec 09, 2011 7:00 pm
by Noso1066
Thank you Jammi, this problem is solved.