making block : mysql_fetch_assoc() expects...
Posted: Sat Apr 07, 2012 10:28 am
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
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
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