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を評価し、その値を返しているということです。最初の部分にデータがあるため、trueと評価され、ブール値trueが返されます。

試す:

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