Page 1 of 1

making block : mysql_fetch_assoc() expects...

Posted: Sat Apr 07, 2012 10:28 am
by BobyFisher
Hi i am a new one here, have been tired with a debuging
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\mywebapp\test\inc\posts.inc.php on line 70
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\mywebapp\test\in\comments.inc.php on line 15
comments.inc.php
<?php  
//кол-во комментарий
function get_comments($nid) {

	$nid = (int)$nid;
	$sql = "SELECT 
	       `comment_body` AS `body`,
		   `comment_user` AS `user`,
		   DATE_FORMAT(`COMMENT_DATE`, '%d%m%Y %H:%i:%s') AS `date`,
		   FROM `comment_news`
		   WHERE `post_id` = {$nid}"; 
		   
		   $comments = mysql_query($sql);
		   $return = array();
		   while(($row = mysql_fetch_assoc($comments)) !==false ){
		   
		   $return[] = $row;
		   
		   }
		   return $return;

}

function add_comment($nid,$user,$body) {
if(valid_nid($nid)===false){
return false;
}
	$nid = (int)$nid;
	$user = mysql_real_escape_string(nl2br(htmlentities($user)));
    $body = mysql_real_escape_string(nl2br(htmlentities($body)));
	mysql_query("INSERT INTO `comment_news` (`post_id`,`commment_body`,`comment_user`,`comment_date`) VALUES ({$nid}, '{$user}', '{$body}',NOW())");
	
	return true;
	
    
}
post.inc.php
<?php 

function valid_newsid($nid) {
$nid =(int)$nid;
$total = mysql_query("SELECT COUNT(`post_id`) FROM `news` WHERE `post_id`  = {$nid}");
$total = mysql_result($total,0);

if ($total !=1) {
return false;
}else {
return true;
}

}

function get_posts() {
                     $sql =  "SELECT `news`.`post_id` AS `id`,
					         `news`.`post_title` AS `title`,
				 LEFT(`news`.`post_body`, 512) AS `preview`,
				               `news`.`post_user` AS `user`,
                              DATE_FORMAT(`news`.`post_date`, '%d%m%Y %H:%i:%s' )	AS `date`,
							  `comment_news`.`total_comments`,
							  DATE_FORMAT(`comment_news`.`last_comment`,'%d%m%Y %H:%i:%s') AS `last_comment`
							  FROM `news`
							  LEFT JOIN(
								SELECT
								   `post_id`,
										COUNT(`comment_id`) AS `total_comments`,
												MAX(`comment_date`) AS `last_comment`
												 FROM `comment_news`
												  GROUP BY `post_id`
												  ) AS `comment_news` 
												  ON `news`.`post_id` = `comment_news`.`post_id`
												   ORDER BY `news`.`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;
}
//считает конкретный пост комменты и заносит в бд
function get_news($nid) {

	$nid = (int)$nid;
	$sql = "SELECT
	 `post_title` AS `title`,
	 `post_body` AS `body`,
	 `post_user` AS `user`,
	 `post_date` AS `date`,
	 FROM `news` 
	 WHERE `post_id` = {$nid}";
	
	$post = mysql_query($sql);
	$post = mysql_fetch_assoc($post);
	$post['comments'] = get_comments($nid);
	
	return $post;
}

function add_news ($name,$title,$body) {

  $name = mysql_real_escape_string(nl2br(htmlentities($name)));
  $title = mysql_real_escape_string(nl2br(htmlentities($title)));
  $body = mysql_real_escape_string(nl2br(htmlentities($body)));
  
  mysql_query("INSERT INTO `news` (`post_user`,`post_title`,`post_body`,`post_date`) VALUES ('{$name}', '{$title}', '{$post_body}', NOW())");


}










?>
i have some diferents here they are:
in db table names: news instead posts
comment_news instead comments
(the connection with db is ok)

and the last thing i have in php it's $nid =(int)$nid (news id) instead $pid
sorry for too many code and maybe for my english it's not quite good

Re: making block : mysql_fetch_assoc() expects...

Posted: Sat Apr 07, 2012 10:58 am
by SkillBuzz
There is so much useless stuff there...


post.inc.php;
line 65 extra ",", dunno if it matters.


Also you might have a typo, but your error mentions an /inc and an /in directory

I recommend you follow the tutorial exactly as is, and make your changes only AFTER it's working for you.

It helps not to rush things, lines 78-80 you just copypasted the whole thing. If you type them yourself, you might actually learn something. =)

Also, messy looking, try to group functions and lines to make it easier on the eyes. :)

Re: making block : mysql_fetch_assoc() expects...

Posted: Sat Apr 07, 2012 2:56 pm
by KnightMaire
resource with boolean means you have a bad query / your query is returning no data (the query returns 'false' hence 'boolean given')

As the user above said, make sure you have correct syntax for queries.

Re: making block : mysql_fetch_assoc() expects...

Posted: Sun Apr 08, 2012 12:56 am
by jacek
Another useful tip is that if you add
echo mysql_error();
after the failing query it will actually tell you what went wrong rather that just that something did go wrong.

Re: making block : mysql_fetch_assoc() expects...

Posted: Sun Apr 08, 2012 2:37 am
by BobyFisher
thanks guys that was't easy but i found a mistakes in sql syntax..
All about two commas
now nightmare is over.. :)

Re: making block : mysql_fetch_assoc() expects...

Posted: Thu Apr 12, 2012 9:08 am
by BobyFisher
just want to ask maybe too easy question for most guys here, how i can add pictures or maybe another media to my posts use a fopen() function or maybe i can do it another way?

Re: making block : mysql_fetch_assoc() expects...

Posted: Thu Apr 12, 2012 10:33 pm
by jacek
BobyFisher wrote:just want to ask maybe too easy question for most guys here, how i can add pictures or maybe another media to my posts use a fopen() function or maybe i can do it another way?
You need to have the images hosted somewhere the browser can get them, this could be an image hosting site like imgur or uploaded to a folder on your site. Then you want to add the html to display the image to post string that goes into the DB.

It's quite a lot of extra complication over the basic blog really.