警告:mysql_fetch_assoc()期望参数1是资源,布尔给定...在第40行

时间:2012-06-08 08:15:51

标签: php

  

可能重复:
  how to prevent this error : Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in … on line 11

一直试图连接到我的数据库以获取博客,但我一直收到此错误:

  

警告:mysql_fetch_assoc()要求参数1为资源,布尔值在第40行的C:\ Program Files \ EasyPHP-5.4.0RC4 \ www \ blog \ core \ inc \ posts.inc.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){
       $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;
}

4 个答案:

答案 0 :(得分:1)

这意味着mysql_query()失败,因此返回 false ,这是一个布尔值。

尝试使用mysql_error()找出问题所在并尝试在PHPMyAdmin中运行SQL查询,看看会发生什么!

此外,您应该开始使用PDOmysqli,mysql_ *函数正在被弃用。

答案 1 :(得分:0)

您的sql查询

可能有错误

那是因为,(来自manual

  

对于返回结果集的SELECT,SHOW,DESCRIBE,EXPLAIN和其他语句,mysql_query()在成功时返回资源,如果出错则返回FALSE。

请再次检查您的查询,然后重试。

此外,你必须注意

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL 
extension should be used. See also MySQL: choosing an API 
guide and related FAQ for more information. 
Alternatives to this function include:

mysqli_query()
PDO::query()

答案 2 :(得分:0)

基本上,你的mysql_query($ sql)返回FALSE。可能是一个错误。尝试在MySQL客户端中运行SQL以查看您出错的地方。

答案 3 :(得分:0)

function get_posts(){
    echo $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";die;

    $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;
}

我回显你的查询。运行这个文件并复制回显的查询并在phpmyadmin sql section上运行它。看看它的工作是否正常?