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

Post here is you are having problems with any of the tutorials.
Post Reply
Noso1066
Posts: 6
Joined: Tue Dec 06, 2011 7:37 pm

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

Post 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?

[syntax=php]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;
}[/syntax]
Image
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

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

Post by Jaami »

Line 5, you forgot a ","

[syntax=php] LEFT(`Posts`.`postBody`, 512) AS `Preview`,[/syntax]
Noso1066
Posts: 6
Joined: Tue Dec 06, 2011 7:37 pm

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

Post 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?
Image
Jaami
Posts: 38
Joined: Wed Nov 23, 2011 11:22 pm
Location: GER

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

Post by Jaami »

Line 9, you forgot a "_" in "DATE FORMAT".

[syntax=php]DATE_FORMAT(`Comments`.`lastComment`, '%d/%m/%y %H:%i:%s') AS [/syntax]
Noso1066
Posts: 6
Joined: Tue Dec 06, 2011 7:37 pm

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

Post by Noso1066 »

Thank you Jammi, this problem is solved.
Image
Post Reply