2012-03-28 48 views
1

可能重複:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL resultmysql_fetch_assoc()問題

我收到此錯誤信息:

警告:mysql_fetch_assoc():提供的參數是不是一個第215行的/.../.../..../index.php中有效的MySQL結果資源

的index.php代碼:

if (isset($_GET['post_id']) && $_GET['post_id'] != '') 
{ 
$p_id = (int) $_GET['post_id']; 
$sql= mysql_query("SELECT * FROM news_post WHERE post_id = '$p_id' AND block = 0 LIMIT 
$offset, $rowsperpage") or mysql_error(); 
} 
else 
{ 
$sql= mysql_query("SELECT * FROM news_post WHERE block = 0 ORDER BY post_id DESC 
LIMIT $offset, $rowsperpage ") or mysql_error(); 
} 
while ($rel = mysql_fetch_assoc($sql))  (Note: this is Line 215) 
{ 
$id = intval($rel['post_id']); 
$sub = ucfirst($rel['subject']); 
$imgname = htmlentities($rel['img_name']); 
$img = htmlentities($rel ['image']); 
$msg = $rel['message']; 
$date = htmlentities($rel['date']); 
$poster = htmlentities($rel['poster']); 
$cat_id = intval($rel['cat_id']); 
$cat_name = htmlentities($rel['cat_name']); 

在我的本地主機,這是確定的,它不顯示任何錯誤消息,但最多時該到我的服務器,然後它顯示錯誤.. 什麼是錯的我的代碼?誰能告訴我正確的方向...
非常感謝。

+0

變化'或mysql_error();''來或死亡(mysql_error());',看看你會得到什麼。 – Josh 2012-03-28 16:36:31

回答

4

您有一個錯誤在下面的行

$sql= mysql_query("SELECT * FROM news_post WHERE block = 0 ORDER BY post_id DESC 
        LIMIT $offset, $rowsperpage ") or mysql_error(); 

mysql_error()返回一個字符串的函數,所以當mysql_query()失敗時,$sql變量將包含任何錯誤被拋出。稍後,您將此字符串變量作爲MySQL資源。

相反,你可能試圖做的是打印出任何字符串mysql_error()返回並終止腳本。這通常通過die()這些腳本完成。

因此,如果您改爲使用以下內容,則腳本將終止並輸出錯誤(如果出現錯誤)。

$sql= mysql_query("SELECT * FROM news_post WHERE block = 0 ORDER BY post_id DESC 
        LIMIT $offset, $rowsperpage ") or die(mysql_error()); 

最後,您聲稱上述工作在您的服務器上,但不在本地主機上,這是不正確的。如果你的服務器發生錯誤,你會得到相同的行爲。

0

這意味着您的查詢出現問題,但是您在代碼中處理錯誤。這意味着查詢犯規得到的結果(受影響的行= 0)

if(mysql_affected_rows($sql) > 0) { 
    //its ok we have result 
} 
else { 
    //mo result found 
}