Blog (including commenting) part 04

Post here is you are having problems with any of the tutorials.
Post Reply
Cozak
Posts: 3
Joined: Sat Jan 12, 2013 11:58 am

Blog (including commenting) part 04

Post by Cozak »

When I try to run blog_list for the test post nothing shows up except for this error: Parse error: syntax error, unexpected end of file in C:\wamp\www\cozakyt\core\inc\comments.inc.php on line 39. So I guess I have two issues. First of all the page is empty, second of all I get that error. Will greatly appreciate any help you can give me.

Asociated code:

get_posts fuction
[syntax=php]function get_posts(){
$sql = "SELECT
`posts`.`post_id` AS `id`,
`posts`.`post_title` AS `title`,
LEFT(`posts`.`post_body`, 512) AS `preview`,
`posts`.`post_user` AS `user`,
DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
`comments`.`total_comments`,
DATE_FORMAT(`comments`.`last_comment` '%d/%m/%Y %H:%i:%s') AS `last_comment`
FROM `posts`
LEFT JOIN (
SELECT
`post_id`,
COUNT(`comment_id`) AS `total_comments`,
MAX(`comment_date`) AS `last_comment`
FROM `comments`
GROUP BY `post_id`
) AS `comments`
ON `posts`.`post_id` = `comments`.`post_id`
ORDER BY `posts`.`post_date` DESC";


$posts = mysql_query($sql);

$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'],
'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
'last_comment' => ($row['last_comment'] === null) ? 'never' : $row['last_comment']
);

}

}
[/syntax]

entire comments.inc file
[syntax=php]<?php

// hämtar alla kommentarer till ett blogginlägg
function get_comments($pid){
$pid = (int)$pid;

$sql = "SELECT
`comment_body` AS `body`,
`comment_user` AS `user`,
DATE_FORMAT(`comment_date`, '%d/%m/%Y %H:%i:%s') AS `date`
FROM `comments`
WHERE `post_id` = {$pid}";

$comments = mysql_query($sql);

$return = array();
while(($row = mysql_fetch_assoc($comments)) !== false){
$return[] = $row;
}

return $return;

// lägger till en kommentar
function add_comment($pid, $user, $body){

if(valid_pid($pid) === false){
return false;
}

$pid = (int)$pid;
$user = mysql_real_escape_string(htmlentities($user));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));

mysql_query("INSERT INTO `comments` (`post_id`, `comment_user`, `comment_body, `comment_date`) VALUES ({$pid}, '{$user}', '{$body}', NOW())");

return true;
}

?>[/syntax]
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Blog (including commenting) part 04

Post by ExtremeGaming »

You are missing a closing } for your get_comments() function which should be after return $return;
<?php while(!$succeed = try()); ?>
Cozak
Posts: 3
Joined: Sat Jan 12, 2013 11:58 am

Re: Blog (including commenting) part 04

Post by Cozak »

Thanks, can't believe I didn't notice that :p
Cozak
Posts: 3
Joined: Sat Jan 12, 2013 11:58 am

Re: Blog (including commenting) part 04

Post by Cozak »

Okay, so I have a new issue with the same piece of code. Instead of getting the previous error I now get this error:

"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\cozakyt\core\inc\posts.inc.php on line 43". It repeats over and over.

I checked the SQL in the get_posts function in phpmyadmin and i get this error: #1582 - Incorrect parameter count in the call to native function 'DATE_FORMAT'. However I don't see any difference between the SQL in the video and mine, maybe I'm just blind but I'd appreciate if anyone could have a look. I posted the related code in the OP.
Kevin123
Posts: 3
Joined: Sun Jan 13, 2013 12:45 pm

Re: Blog (including commenting) part 04

Post by Kevin123 »

man i have same problem mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\news\core\inc\comments.inc.php on line 33
'ExtremeGaming' tells me to 'Your second undefined variable error is because you have:'
[syntax=php]$comments = mysql_query($pid);[/syntax]
'In your while loop you are using:'
[syntax=php]While (($row = mysql_fetch_assoc($comment)) !== false){[/syntax]

but i cant understand him maybe you can ? ;)

i do what he tells me but comments dosnt work
example
[syntax=php]While (($comments = mysql_query($pid)) !== false){[/syntax]
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Blog (including commenting) part 04

Post by ExtremeGaming »

@Cozak, you're missing a comma here. There needs to be 2 parameters.
[syntax=sql]
DATE_FORMAT(`comments`.`last_comment` '%d/%m/%Y %H:%i:%s') AS `last_comment`
[/syntax]

@Kevin check the thread you started.
<?php while(!$succeed = try()); ?>
Post Reply