0

This question is similar to this one: Why does mysql_query() return TRUE with a SELECT statement?. I execute a SELECT mysql query in php, but it returns true. I've been starting at it for half an hour now and I'm completely positive it is a select query and it returns true. The obvious fix for the similar question was to check if the connection was still alive. The difference for me is, the mysql_error is empty. Here are the parts of my code that cause the error:

public function query($query, $params = array())
{
    $params['prefix'] = 'unpirate_';

    foreach($params as $key => $param)
    {
        $query = str_replace('{' . $key . '}',
            mysql_real_escape_string($param), $query);
    }

    echo($query);
    return mysql_query($query, $this->conn) or $this->error($query);
}

public function fetch_all($query, $params = array())
{
    $result = $this->query($query, $params);

    if($result === true)
        die('"' . mysql_error() . '"');
    (...)
}

The query that is echo'd is a valid query (SELECT * FROM [table] WHERE event_id = "54") and the only other thing that is echo'd is "", thus the result is true and the mysql_error is empty. Do you guys have any idea what is going on? Please correct me if my conclusion is not right.

PHP version: PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:19:58)

MySQL version: mysql Ver 14.14 Distrib 5.5.24, for debian-linux-gnu (x86_64) using readline 6.2

4

1 回答 1

5
return mysql_query($query, $this->conn) or $this->error($query);

您的返回值是 OR 语句。

发生的事情是 PHP 正在评估 OR 并返回它的值,而不是代码本身;因为第一部分有数据,所以它评估为真,并返回一个布尔值真。

尝试:

$retVal = mysql_query($query, $this->conn);
if (false === $retVal) {
    return $this->error($query);
}
return $retVal;
于 2012-08-17T14:56:42.260 回答