SQL查询出错?

时间:2014-01-21 08:44:18

标签: php sql

警告: mysql_fetch_assoc()要求参数1为资源,布尔值在第104行的C:\ xampp \ htdocs \ Arsenal Website \ ArsenalTables.php中给出

我有一个非常相似的查询,实际上它与不同的变量名相同,但我无法理解为什么这不起作用?

$customtable_query = "SELECT * FROM `arsenaltable`\n" . "ORDER BY `arsenaltable`.`points` DESC LIMIT 0, 30 ";

$customtable_result = mysql_query($customtable_query);

while ($customtable_row = mysql_fetch_assoc($customtable_result))
{                   
    $played = $customtable_row['played'];
    echo $played;
}

3 个答案:

答案 0 :(得分:1)

  1. 删除不必要的单引号(它们通常用于值而不是列名)。
  2. 删除换行符

    $customtable_query = "SELECT * FROM arsenaltable ORDER BY arsenaltable,points DESC LIMIT 0, 30 ";
    
    $customtable_result = mysql_query($customtable_query);
    
    while ($customtable_row = mysql_fetch_assoc($customtable_result)) {
    
        $played = $customtable_row['played'];
        echo $played;
    
    }
    
  3. 希望这有帮助!

    • 与解决方案没有直接关系,但是mysql_query已被弃用,最好使用mysqli_query代替(甚至更好,根据我的口味,PDO)。

答案 1 :(得分:1)

您可能希望查看mysql_error()的特定错误。 你的mysql_query()调用返回false,这就是为什么错误说mysql_fetch_assoc()中的参数是一个布尔值。

答案 2 :(得分:0)

还传递mysql_query中的连接变量。希望这会有所帮助。

$con=mysql_connect("localhost", "root","password");
$customtable_query = "SELECT * FROM `arsenaltable`\n" . "ORDER BY `arsenaltable`.`points`    DESC LIMIT 0, 30 ";

$customtable_result = mysql_query($customtable_query,$con);

while ($customtable_row = mysql_fetch_assoc($customtable_result))
{                   
$played = $customtable_row['played'];
echo $played;
}