2013-10-25 69 views
0

做一個相當常規的查詢時,我突然發現了以下錯誤: Warning</b>: mysql_num_rows(): supplied argument is not a valid MySQL result resourcePHP MySQL的問題

這裏是我的代碼:

$query = "SELECT * FROM ImageGalleryPhotos ORDER BY ImageOrder"; 
$result = mysql_query($query); 

/* create one master array of the records */ 
$imagesArray = array(); 

// here is the line where I'm getting the error I listed up top: 
if(mysql_num_rows($result)) {  
    while($image = mysql_fetch_assoc($result)) { 
     $imagesArray[] = array('image'=>$image); 
    } 
} 

echo json_encode($imagesArray); 

// And here I'm getting error # 2 (see below): 
mysql_free_result($result); 

?> 

錯誤#2:<b>Warning</b>: mysql_free_result(): supplied argument is not a valid MySQL result resource

奇怪事情是,當我做沒有「ORDER BY」業務的搜索時,一切正常,就像這樣:

$query = "SELECT * FROM ImageGalleryPhotos"; 
+2

如果在運行查詢後輸出mysql_error(),它會告訴你到底是什麼問題。你假設你的查詢已經工作,並且根本不做任何錯誤檢查。 – andrewsi

+0

爲什麼在'mysql_query()'行的末尾沒有'或者死掉(mysql_error())',所以你會看到錯誤信息? – Barmar

+2

聽起來像你的'ORDER BY'有錯誤。你真的確定你的表中有一個ImageOrder列嗎? – Travesty3

回答

0

請先嚐試:

$query = "SELECT * FROM ImageGalleryPhotos ORDER BY ImageOrder"; 
$result = mysql_query($query) or die(mysql_error()); 

看起來你是在你的查詢時出現錯誤。

+0

我的'ImageOrder'列中的一些值是'NULL',我認爲這絕不是一件好事。所以這可能與它有關。否則,我做了複製粘貼你的兩行代碼,所以我可以使用'或者死(mysql_error())'位,並且我突然沒有收到任何錯誤 - 所以我現在都很好。 (也許我的代碼中還有一些'垃圾'字符?)無論哪種方式,謝謝你:-) – sirab333

+0

很高興我能幫忙!快樂編碼:) – Rachid