Having trouble with part 04 of blog tutorial

Post here is you are having problems with any of the tutorials.
Post Reply
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Having trouble with part 04 of blog tutorial

Post by brisk »

Everything working so far apart from retrieving the dummy data in the comments table.

I.e. to display the "(0 comments, last comment never)" part.

I have the database and tables the exact same names, only thing that differs I have all the functions and a DB connect and selection function defined earlier on. Not that any of this matters because it's connecting and getting some data, just no the comment part.

http://betterphp.co.uk/video.html?vid=dUKT0Va4ZhQ
<div class="facts_bg">

	<h1>FACTS</h1>
    
    <hr />
    
    <?php	
	
		$posts = get_posts();
		foreach ($posts as $post){
			?>
            <h2><?php echo $post['title']; ?></h2>
            <h4>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?></h4>
            <h4>(<?php echo $post['total_comments']; ?> comments, last comment <?php echo $post['last_comment']; ?>)</h4>
            
            <hr />
            
            <p><?php echo $post['preview']; ?></p>
            <?php	
		}
		
				
	?>  
	
</div>
	//fetches a summary of all the blog posts
	function get_posts(){
		
		connect();
		selectDB();
		
		$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']
				
			);	
		}
		
		return $rows;
		
	}
	
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Re: Having trouble with part 04 of blog tutorial

Post by brisk »

I tried going on but at the end of part 5 i'm getting

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\budgie\organised\functions\functions.php on line 581

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\budgie\organised\functions\functions.php on line 653

Notice: Undefined index: title in C:\xampp\htdocs\budgie\organised\facts_read.php on line 52
By
Notice: Undefined index: user in C:\xampp\htdocs\budgie\organised\facts_read.php on line 53
on
Notice: Undefined index: date in C:\xampp\htdocs\budgie\organised\facts_read.php on line 53

LINE 581 IN - VALID_PID
$total = mysql_result($total, 0);
LINE 653 - GET_POST($PID)
$post = mysql_query($sql);
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Having trouble with part 04 of blog tutorial

Post by jacek »

This usually means that there is an error with your SQL which causes mysql_query to return false.

If you add
echo mysql_error();
under the call to mysql_query() it should tell you what the problem is.

Also, you don't want to be connecting to the database for every query so you can move the connect(); and select_db() functions outside of that function.
Image
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Re: Having trouble with part 04 of blog tutorial

Post by brisk »

Mhm, but what about tge first part? The number of comments isn't displaying

Want to get that bit sortes first
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Having trouble with part 04 of blog tutorial

Post by jacek »

It could be a result of the query failing, try fixing the obvious errors first and then move on to the weird problems ;)
Image
Post Reply