Blog(including commenting) issue

Post here is you are having problems with any of the tutorials.
Post Reply
merazzauk
Posts: 5
Joined: Sat Jan 12, 2013 9:56 pm

Blog(including commenting) issue

Post by merazzauk »

Hi, I am receiving the following errors when trying to test my blog_list page:

Notice: Undefined variable: sql in C:\xampp\htdocs\caddingtoncc\blog\core\inc\posts.inc.php on line 36

Notice: Undefined variable: sql in C:\xampp\htdocs\caddingtoncc\blog\core\inc\posts.inc.php on line 38

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\caddingtoncc\blog\core\inc\posts.inc.php on line 41

Here is the php syntax from the posts.inc.php file
<?php

// checks if the given post id is in the table
function valid_pid($pid){
	$pid = (int)$pid;
	
	$total = mysql_query("select count('post_id') from 'post' where 'post_id' = {$pid}");
	$total = mysql_result($total, 0);
	
	if ($total !=1){
		return false;
	}else{
		return true;
	}
}

// fetches a summary of all the blog posts
function get_posts(){
	$sql-"select
			`post`.`post_id` as `id`,
			`post`.`post_title` as `title`,
			left(`post`.`post_body`, 512) as `preview`,
			`post`.`post_user` as `user`,
			date_format(`post`.`post_date`, `%d/%m/%Y %h:%i:%s`) as `date`,
			`comment`.`total_comments`,
			date_format(`comment`.`last_comment`, `%d/%m/%Y %h:%i:%s`) as `last comment`
			from `post`
			left join (
			SELECT `post_id`, 
			count(`comment_id`) as `total_comments`,
			max(`comment_date`) as `last_comment`
			from `comment`
			group by `post_id`
			) as `comment`
			on `post`.`post_id` = `comment`.`post_id`
			order by `post`.`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;
}

// fetches a single post from the table
function get_post($pid){
	$pid = (int)$pid;
	
	$sql = "select
					`post_title` as `title`,
					`post_body` as `body`,
					`post_user` as `user`,
					`post_date` as `date`
			from `post`
			where `post_id` = {$pid}";
			
	$post = mysql_query($sql);
	$post = mysql_fetch_assoc($post);
	$post['comments'] = get_comments($pid);
	
	return $post;
}

// adds a new blog entry
function add_post($name, $title, $body){
	$name = mysql_real_escape_string(htmlentities($name));
	$title = mysql_real_escape_string(htmlentities($title));
	$body = mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("insert into `post` (`post_user`, `post_title`, `post_body`, `post_date`) values ('{$name}','{$title}','{$body}',now())");
}

?>
I have run the query in the database and it is returning the data that I have entered.

Could you please help.

Many thanks,

Gary
User avatar
KnightMaire
Posts: 29
Joined: Thu May 05, 2011 9:03 pm

Re: Blog(including commenting) issue

Post by KnightMaire »

Look closely at how you set the $sql variable. :lol:
Last edited by Helx on Sun Jan 13, 2013 7:11 am, edited 1 time in total.
Reason: No need to laugh.
merazzauk
Posts: 5
Joined: Sat Jan 12, 2013 9:56 pm

Re: Blog(including commenting) issue

Post by merazzauk »

Yep, i'm quite new to php, but very experienced at typos I'm afraid!! :lol:

That has fixed the first two errors but not the mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in warning...

Any ideas?
merazzauk
Posts: 5
Joined: Sat Jan 12, 2013 9:56 pm

Re: Blog(including commenting) issue

Post by merazzauk »

Ok, so it turns out that my sql wasn't quite correct!!

Fixed it now :)
User avatar
KnightMaire
Posts: 29
Joined: Thu May 05, 2011 9:03 pm

Re: Blog(including commenting) issue

Post by KnightMaire »

Right, if you fetches every return a resource, 90% of the time its a query problem which you can find the error using mysql_error().
merazzauk
Posts: 5
Joined: Sat Jan 12, 2013 9:56 pm

Re: Blog(including commenting) issue

Post by merazzauk »

Well, I have now come across another issue!!

Whenever I link from my posts list to the blog_read page I am getting my invalid post ID message...

Here is the syntax for the blog_read file:
<?php
include 'core/init.inc.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>News|Caddington Cricket Club</title>
</head>

<body>
<div>
<?php

if (isset($_get['pid']) === false||valid_pid($_get['pid'])=== false) {
		echo 'Invalid post ID.';
        }else{
		$post = get_post($_get['pid']);	

?>

<h2><?php echo $post['title']; ?></h2>
<h4><?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']);?> comments)</h4>

<hr />

<p></p>

<hr />

<form action="" method="post">
	<p>
    	<label for="user">Name</label>
        <input type="text" name="user" id="user" />
    </p>
    <p>
    	<textarea name="body" rows="20" cols="60"></textarea>
    </p>
    <p>
    	<input type="submit" value="Add Comment" />
    </p>
   </form>
   <?php
		}
   ?>
   
</div>   
</body>
</html>
...and for the posts.inc file:
<?php

// checks if the given post id is in the table
function valid_pid($pid){
	$pid = (int)$pid;
	
	$total = mysql_query("select count(`post_id`) from `post` where `post_id` = {$pid}");
	$total = mysql_result($total, 0);
	
	if ($total != 1){
		return false;
	}else{
		return true;
	}
}

// fetches a summary of all the blog posts
function get_posts(){
	$sql=	"select
			`post`.`post_id` as `id`,
			`post`.`post_title` as `title`,
			left(`post`.`post_body`, 512) as `preview`,
			`post`.`post_user` as `user`,
			date_format(`post`.`post_date`, '%d/%m/%Y %h:%i:%s') as `date`,
			`comment`.`total_comments`,
			date_format(`comment`.`last_comment`, '%d/%m/%Y %h:%i:%s') as `last_comment`
			from `post`
			left join (
			SELECT `post_id`, 
			count(`comment_id`) as `total_comments`,
			max(`comment_date`) as `last_comment`
			from `comment`
			group by `post_id`
			) as `comment`
			on `post`.`post_id` = `comment`.`post_id`
			order by `post`.`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;
}

// fetches a single post from the table
function get_post($pid){
	$pid = (int)$pid;
	
	$sql = "select
					`post_title` as `title`,
					`post_body` as `body`,
					`post_user` as `user`,
					`post_date` as `date`
			from `post`
			where `post_id` = {$pid}";
			
	$post = mysql_query($sql);
	$post = mysql_fetch_assoc($post);
	$post['comments'] = get_comments($pid);
	
	return $post;
}

// adds a new blog entry
function add_post($name, $title, $body){
	$name = mysql_real_escape_string(htmlentities($name));
	$title = mysql_real_escape_string(htmlentities($title));
	$body = mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("insert into `post` (`post_user`, `post_title`, `post_body`, `post_date`) values ('{$name}','{$title}','{$body}',now())");
}

?>
I have had a good look through, but I'm a bit of a novice and I cant spot anything that differs from the tutorial... There is a record in the database and running the sql from the valid_pid function (replacing the {$pid} with 1 returns the record.

Please help!! :)

Gary
merazzauk
Posts: 5
Joined: Sat Jan 12, 2013 9:56 pm

Re: Blog(including commenting) issue

Post by merazzauk »

Never mind!! It's because I was using lowercase for $_GET...
User avatar
KnightMaire
Posts: 29
Joined: Thu May 05, 2011 9:03 pm

Re: Blog(including commenting) issue

Post by KnightMaire »

Also make sure you get into the habit of putting spaces between evaluators (===, ||, &&, etc) - otherwise you could run into errors some time in the future and not be able to find the error because you are so used to not putting spaces.

Just a note from personal experience, since I used to do the same thing many years ago when i first started coding in PHP.
Post Reply