Question

I'm using PHP 5.4.4 and MySQL 5.5.31 on Debian 6. I have the following code:

file_put_contents("dump.log", $check_username); // To ensure the variable is set, which it is
$database->query('SELECT * FROM users WHERE username = :username', array(':username' => $check_username));
die("Query didn't crash");

The query function is here:

public function query($query, $bind = null) {
    global $pdo;
    /* Prepare Statment */
    try {
        $this->statement = $this->pdo->prepare($query);
    }
    catch(Exception $e) {
        echo "MySQL Exception -> " . $e->getMessage();
    }
    /* Execute Query */
    return $this->statement->execute($bind); // Crash here
}

At this point apache2 crashes with the following error and doesn't return anything to the client:

*** glibc detected *** /usr/sbin/apache2: free(): invalid pointer: 0x00007fb3b61dbbf8 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x76d76)[0x7fb3b3840d76]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x6c)[0x7fb3b3845aac]
/usr/lib/php5/20100525/mysqlnd.so(_mysqlnd_pefree+0x4e)[0x7fb3ac14ac7e]
/usr/lib/php5/20100525/mysqlnd.so(+0x2645f)[0x7fb3ac15e45f]
/usr/lib/php5/20100525/mysqlnd.so(+0x278e0)[0x7fb3ac15f8e0]
/usr/lib/php5/20100525/mysqlnd.so(+0x2614e)[0x7fb3ac15e14e]
/usr/lib/php5/20100525/pdo_mysql.so(+0x4f7f)[0x7fb3a99b0f7f]
/usr/lib/php5/20100525/pdo.so(+0xc39a)[0x7fb3abf2839a]
/usr/lib/apache2/modules/libphp5.so(zend_objects_store_del_ref_by_handle_ex+0x293)[0x7fb3b0818d63]
/usr/lib/apache2/modules/libphp5.so(zend_objects_store_del_ref+0x13)[0x7fb3b0818d83]
/usr/lib/apache2/modules/libphp5.so(_zval_ptr_dtor+0x3a)[0x7fb3b07e312a]
/usr/lib/apache2/modules/libphp5.so(zend_hash_destroy+0x38)[0x7fb3b07ffe88]
/usr/lib/apache2/modules/libphp5.so(zend_object_std_dtor+0x2c)[0x7fb3b0812fbc]
/usr/lib/apache2/modules/libphp5.so(zend_objects_free_object_storage+0x9)[0x7fb3b0813049]
/usr/lib/apache2/modules/libphp5.so(zend_objects_store_free_object_storage+0x87)[0x7fb3b0818897]
/usr/lib/apache2/modules/libphp5.so(+0x35668c)[0x7fb3b07e368c]
/usr/lib/apache2/modules/libphp5.so(+0x3650b5)[0x7fb3b07f20b5]
/usr/lib/apache2/modules/libphp5.so(php_request_shutdown+0x1da)[0x7fb3b0791a3a]
/usr/lib/apache2/modules/libphp5.so(+0x40f35f)[0x7fb3b089c35f]
/usr/sbin/apache2(ap_run_handler+0x70)[0x7fb3b4670ab0]
/usr/sbin/apache2(ap_invoke_handler+0xce)[0x7fb3b467444e]
/usr/sbin/apache2(ap_process_request+0x1c8)[0x7fb3b4682078]
/usr/sbin/apache2(+0x52f28)[0x7fb3b467ef28]
/usr/sbin/apache2(ap_run_process_connection+0x68)[0x7fb3b46786a8]
/usr/sbin/apache2(+0x5b1b7)[0x7fb3b46871b7]
/usr/sbin/apache2(+0x5b4ca)[0x7fb3b46874ca]
/usr/sbin/apache2(ap_mpm_run+0xc24)[0x7fb3b4688154]
/usr/sbin/apache2(main+0xb40)[0x7fb3b465ca90]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd)[0x7fb3b37e8ead]
/usr/sbin/apache2(+0x2fa29)[0x7fb3b465ba29]

The code worked on Windows with XAMPP and PHP 5.3.8 and the only other difference is that my Debian has MySQLnd installed, while the local one does not. PDO reports its active driver to be mysql, but the tracelog suggests it's using native driver.

Some googling suggested that native driver requires results to be cleared between queries, however, I highly doubt this is the case since (a) apache crashes on first query, before there are any results and (b) I have tried clearing them with both clearCursor and fetch loops. I have also tried unsetting the statement before preparing it.

Question: How do I fix the query function to prevent it from crashing the server?

Was it helpful?

Solution

After reading this great Q&A, I noticed the PDO connection was set to be persistent by the original developer (who is in the project but inactive). Changing array(PDO::ATTR_PERSISTENT => true, to array(PDO::ATTR_PERSISTENT => false, fixed the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top