Why does mysql_query() return TRUE with a SELECT statement?

后端 未结 7 1759
日久生厌
日久生厌 2020-12-20 15:24

According to the manual of mysql_query() and to everything I know about this function that I used so many times, it can either return a resource or FALSE if the

相关标签:
7条回答
  • 2020-12-20 15:39

    You haven't mentioned data lengths but you should check to see if this is called by an exceeding of max_allowed_packet.

    When a MySQL client or the mysqld server receives a packet bigger than max_allowed_packet bytes, it issues a Packet too large error and closes the connection. With some clients, you may also get a Lost connection to MySQL server during query error if the communication packet is too large.

    [...]

    Both the client and the server have their own max_allowed_packet variable, so if you want to handle big packets, you must increase this variable both in the client and in the server.

    http://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html

    Not sure how mysql_query return value is affected by this but it's worth investigating. Setting it in my.cnf to see if it fixes the problem would be the best place to start.

    0 讨论(0)
  • 2020-12-20 15:54

    For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

    For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

    (from http://php.net/manual/en/function.mysql-query.php)

    You say that SELECTs are returning boolean true? Not just a resource that isn't zero :) ?

    0 讨论(0)
  • 2020-12-20 15:56

    Let's look at this the other way, When mysql_query() returns FALSE it means that for what ever reason, the attempt to query has failed. Meaning that even before it got to do you're query something went wrong, as such there has been no effect on the database, or that the query was unable to even get to a point where it could see the database.

    0 讨论(0)
  • 2020-12-20 15:57

    mysql_query doesn't return FALSE, let alone TRUE. It returns a resource id that if equates to zero, con be taken as FALSE. My example is to illustrate boolean results

    $resource = mysql_query($query, $handle);
    if (!$resource) throw some exception;
    if (!$resource && strpos($query, 'SELECT')) {
    throw new Exception('mysql_query() returned TRUE for SELECT');
    }
    

    PHP Booleans

    0 讨论(0)
  • 2020-12-20 15:58

    If webbiedave isn't on the right track, there's only one codepath that allows for this situation in the php source:

    #if MYSQL_VERSION_ID < 32224
    #define PHP_MYSQL_VALID_RESULT(mysql)       \
        (mysql_num_fields(mysql)>0)
    #else
    #define PHP_MYSQL_VALID_RESULT(mysql)       \
        (mysql_field_count(mysql)>0)
    #endif
    

    ...

    if (!mysql_result) {
        if (PHP_MYSQL_VALID_RESULT(mysql->conn)) { /* query should have returned rows */
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
            RETURN_FALSE;
        } else {
            RETURN_TRUE; // <<< this case
        }
    }
    

    I would consider this a bug. Especially since there's no real way to verify this - mysql_num_fields in the PHP code uses the resource that you're not getting, not the connection.

    Although it's still weird that the C version of mysql_query returns zero on lost connection - if you're able to, try the following patch and reinstall the mysql extension:

    Index: ext/mysql/php_mysql.c
    ===================================================================
    --- ext/mysql/php_mysql.c       (revision 311719)
    +++ ext/mysql/php_mysql.c       (working copy)
    @@ -1485,6 +1485,9 @@
                    if (PHP_MYSQL_VALID_RESULT(mysql->conn)) { /* query should have returned rows */
                            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
                            RETURN_FALSE;
    +               } else if( mysql_errno(mysql->conn) != 0 ) {
    +                       php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
    +                       RETURN_FALSE;
                    } else {
                            RETURN_TRUE;
                    }
    
    0 讨论(0)
  • 2020-12-20 16:05

    What James was trying to say is that the function mysql_query() simply returns true if the query has been taken and executed properly. It has nothing to do with the results the query yields.

    If you want to actually fetch the results, use mysql_fetch_assoc()/ mysql_fetch_array()/ mysql_results().

    0 讨论(0)
提交回复
热议问题