Page 1 of 1

Having trouble with part 04 of blog tutorial

Posted: Thu Jan 26, 2012 5:21 pm
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;
		
	}
	

Re: Having trouble with part 04 of blog tutorial

Posted: Thu Jan 26, 2012 6:38 pm
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);

Re: Having trouble with part 04 of blog tutorial

Posted: Sat Jan 28, 2012 10:28 pm
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.

Re: Having trouble with part 04 of blog tutorial

Posted: Sun Jan 29, 2012 4:03 pm
by brisk
Mhm, but what about tge first part? The number of comments isn't displaying

Want to get that bit sortes first

Re: Having trouble with part 04 of blog tutorial

Posted: Tue Jan 31, 2012 6:11 pm
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 ;)