PHP Tutorial: Blog (Including Commenting) [part 03] #Error#
Posted: Tue Dec 06, 2011 9:44 pm
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?
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; }