2012-06-08 43 views
-1

Possible Duplicate:
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行

一直試圖連接到我的數據庫的博客,但我不斷收到此錯誤:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.4.0RC4\www\blog\core\inc\posts.inc.php on line 40

能有人幫助我out..here是我的代碼...

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; 
} 
+0

你是否使用了這個錯誤?這應該是你的第一道防線。它通常意味着在調用'mysql_fetch_assoc()'之前運行的查詢有錯誤。嘗試使用'mysql_error()'來找出這個錯誤是什麼。 –

+0

請使用mysql_error來檢查。 – xdazz

+0

感謝您的幫助人,我想出了連接db文件中的問題... – alex

回答

1

這意味着mysql_query()失敗,所以它返回,這是一個布爾值。

嘗試使用mysql_error()找出問題所在,然後嘗試在PHPMyAdmin中運行SQL查詢,看看會發生什麼!

此外,您應該開始使用PDOmysqli,mysql_ *函數正在被棄用的過程中。

+0

如果他需要程序代碼,他也可以使用'mysqli_ *'將它添加到你的答案中,如果你提供手動鏈接對OP,IMO有幫助。順便說一下,我認爲有SQL語法錯誤。 – Leri

+0

嗯..我已經說過PDO或** mysqli **之前,你說過,並且在你說之前已經有鏈接... – Jeroen

+0

哦,對不起。我現在看到了更新的答案。 – Leri

0

你可能有一個錯誤到你sql查詢

這是因爲,(從manual

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

那麼再次檢查您的查詢,然後重試。

而且,你必須要注意的是

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() 
+0

感謝您的幫助人,我想出了我的連接數據庫文件中的問題... – alex

0

基本上,你的mysql_query($ sql中)與FALSE返回。可能是一個錯誤。嘗試在MySQL客戶端中運行您的SQL,以查看您錯誤的位置。

+0

感謝您的幫助人,我想通了在我的連接數據庫文件中的問題... – alex

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

我贊同你的query.run這個文件,並複製呼應查詢和phpMyAdmin的SQL運行section.than正確或不看它的工作?

+0

它說,查詢成功執行...所以最新的問題 – alex

+0

@alex在'mysql_query($ sql)'後面添加'或死(mysql_error());''。 – Leri

+0

嘿PLB,錯誤是沒有選擇數據庫... – alex

相關問題