在ENUM类型的SQL语句中使用WHERE会破坏mysql_query

时间:2012-07-21 03:35:29

标签: php mysqli

守则:

        $query = "SELECT * FROM tweets WHERE read=n";
        $result = mysqli_query($mysqli,$query);

        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        echo $row["from_user"], $row["text"];

上面的代码给了我这个:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /index.php on line 16 

以下代码不会:

        $query = "SELECT * FROM tweets";
        $result = mysqli_query($mysqli,$query);

        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        echo $row["from_user"], $row["text"];

“读取”字段为ENUM('y','n')。

2 个答案:

答案 0 :(得分:2)

read是MySQL中的保留关键字。因为它是列的名称,所以在WHERE子句中添加反引号,这应该可以解决问题:

SELECT * FROM tweets WHERE `read` = 'n';

答案 1 :(得分:1)

您需要引用ENUM值,以及转义readwhich is a reserved word in MySQL

$query = "SELECT * FROM tweets WHERE `read` = 'n'";