警告:mysql_fetch_array()期望参数1是资源,给定布尔值

时间:2012-05-22 14:46:47

标签: php

  

可能重复:
  PHP: Warning: sort() expects parameter 1 to be array, resource given

我有以下PHP代码,但是收到此错误。我尝试了不同的解决方案,但它似乎没有用。任何帮助将不胜感激。

<?php
INCLUDE 'functions.php' ;

$host = 'localhost';    
$id = '***';    
$pwd = '***';    
$db = '****'; 

$myconnection = connect_db($host, $id, $pwd, $db);

$SortOn = $_POST["SortOn"];    
$SortIn = $_POST["SortIn"];


$sql = "SELECT ID, DateTime, FirstName, LastName, AdditionalInformation, Category1,    Category2, Category3, Category4, Category5, Pending, Approved, Disapproved, WebsiteName, WebsiteURL FROM 'websites' ORDER BY $SortOn $SortIn";
echo "<table border=\"1\"><tr><th>ID</th><th>Date & Time</th><th>First Name</th><th>Last Name</th><th>Additional Information</th><th>Category 1</th><th>Category 2</th><th>Category 3</th><th>Category 4</th><th>Category 5</th><th>Pending</th><th>Approved</th><th>Disapproved</th><th>Website Name</th><th>Website URL</th></tr>";

$result = mysql_query($sql);            



while ( $row = mysql_fetch_array($result))            
{                
    echo "<tr>";        
    for ( $column = 0;$column < count($row);$column++)        
    {            
        echo "<td>" . $row[$column] . "</td>";        
    }        
    echo "</tr>";    


}            
echo "</table>";

?>   

3 个答案:

答案 0 :(得分:0)

这个变量$result给你一个布尔值(可能它会返回一个False或类似的东西,告诉你查询没有做好“执行”而不是resource as他预料到了。

所以你必须使用类似mysql_error()之类的内容来检索原因并更正上述代码。

修改 看看您的代码,错过了,字段

中的order by

答案 1 :(得分:0)

发生错误时,

mysql_query返回布尔值false。所以看起来你必须修复你的查询。 您可以使用mysql_error来确定错误是什么。

答案 2 :(得分:0)

确保您也拥有有效的数据库连接。尝试connect_db($ host,$ id,$ pwd,$ db)或die(mysql_error())。另外,按照其他人的建议在sql语句中使用die。