2013-10-25 67 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