2012-05-09 47 views
0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in selectPHP:SQL蜱沒有任何意義

我的代碼是工作的罰款我最後一次檢查,但現在它似乎已經打破。以下是錯誤:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/nerblog/public_html/nerblog/arch.php on line 4 

...這裏是第4行代碼:

$q = mysql_query("SELECT * FROM main WHERE month='$month' AND year='$year' LIMIT 12 ORDER BY id DESC"); 

    while ($sql = mysql_fetch_array($q)) 
+0

這是一個布爾值,因爲你的查詢是無效的 –

回答

3

你的SQL查詢失敗。 「令」,查詢結束後嘗試將「限制」命令:

SELECT * FROM main WHERE month='$month' AND year='$year' ORDER BY id DESC LIMIT 12 
+0

它的工作原理,非常感謝你! –

1

mysql_error()存在是有原因的。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id DESC' at line 1

這意味着它沒有期望看到ORDER BY你把它放在哪裏。原因是LIMIT應該在ORDER BY之後來到,而不是之前。

2

根據文檔:

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

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

的mysql_fetch_array()函數需要通過的mysql_query返回的結果資源。如果調用失敗,它將返回false,這就是你傳遞給你的mysql_fetch_array()函數的原因,因此它失敗了。

問題實際上在你的SQL中。你需要有最後在查詢的LIMIT條款本身:

SELECT * FROM main WHERE month='$month' AND year='$year' ORDER BY id DESC LIMIT 12 

的一種方式,以確保您的查詢不下來滲入其他代碼錯誤,是先檢查值:

$q = mysql_query("SELECT * FROM main WHERE month='$month' AND year='$year' ORDER BY id DESC LIMIT 12"); 

if($q != FALSE) 
{ 
    // Query didn't fail, so get results 
    while ($result = mysql_fetch_array($q)) 
    { 
     // Do something with $result 
    } 

}