Problem with the blog tutorial...

Ask about a PHP problem here.
Post Reply
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Problem with the blog tutorial...

Post by sturekdrf »

So been nudging my way through the tutorial and minus the few typo's that I had to go back and fix everything "seems" to be working fine. EXCEPT the get_posts function seems to not work. I am not getting any errors when loading the page, but I am also not getting anything being displayed. I ran the code through myphpadmin came up fine, I did put in a test post but when I ran the code it came up with 0-0 rows which doesnt seem right but i am pretty sure I have the database setup right.

My orignal database name instead of blog is newssys but I don't see how that matters I have the comments and posts tables and nothing is wrong with them.

posts has
post_id int(6) AI Primary
Post_title varchar255
post_body text
post_user varchar 32
post_date datetime

even changed the collaition to latin_swedish_ci for the ones that had it to match teh tutorials. nothing. I can verify that the test post is in there as well.

here is the function and the code from blog_list.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){
		$row[] = 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;
}
<div>
<?php

	$posts = get_posts();
	
	foreach ($posts as $post){
	?>
	<h2><?php $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>
Last edited by sturekdrf on Fri Jun 15, 2012 10:07 pm, edited 1 time in total.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problem with the blog tutorial...

Post by Temor »

your get_posts function is never returning the array.
you need to add this bit:
return $rows;
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Problem with the blog tutorial...

Post by sturekdrf »

Sorry I guess I did a bad job at copy and pasting apparently that function is in there. I updated the script above to reflict the entire function my apologies on that. But when i run it through the database sql it says it not getting any rows or 0-0 rows but its also showing no errors. Which leads me to believe this is a problem with the database, but as far as i can tell everything i name correctly.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Problem with the blog tutorial...

Post by Temor »

try adding mysql_error(); underneath where you run the query.
that's all I can think of for now. I'll look at it again after I got some sleep :)

/Edit, I'm well rested now! :)

if you add print_r($rows); before the return line, does it give you anything?

Can you show me your ini file? The one where you're establishing your db connection.

/Edit 2
jacek wrote:
while (($row = mysql_fetch_assoc($posts)) !==false){
    $row[] = array (
You should be adding to the $rows array here not $row.
I should have seen that :oops:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Problem with the blog tutorial...

Post by jacek »

while (($row = mysql_fetch_assoc($posts)) !==false){
    $row[] = array (
You should be adding to the $rows array here not $row.
Image
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Problem with the blog tutorial...

Post by sturekdrf »

damnit, knew it was a typo thanks.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Problem with the blog tutorial...

Post by jacek »

That is a hard one to spot too ! Glad you sorted it ;)
Image
sturekdrf
Posts: 40
Joined: Fri Jun 15, 2012 8:25 pm

Re: Problem with the blog tutorial...

Post by sturekdrf »

Me nothing lol, I didnt do anything other than post it in here to have someone more knowledgeable help. I knew basic php, enough to get my by with forums and decided to really take the extra step and learn it thoroughly to improve my web design skills since I started up a charity site, and now working on a personal site. Found out there was no good customization open source news system and most seemed really open to attacks of some kind, so I decided to take up learning the language. Gota say though your stuff is probably the best I have come across over the net including youtube. But honestly doing much with the current system you have made is still a bit over my head so been going over all your other tutorials starting with the basics. Good stuff.
Post Reply