简体   繁体   中英

CodeIgniter Random PHP Fatal error: Call to a member function … on a non-object in … mysqli_driver.php

I find many posts that are close to this but none are the same. I have a heavily used CodeIgniter app that has maybe a couple dozen, totally random, fatal errors logged to the error log per day.

PHP Fatal error: Call to a member function real_escape_string() on a non-object in /system/database/drivers/mysqli/mysqli_driver.php

or

PHP Fatal error: Call to a member function query() on a non-object in /system/database/drivers/mysqli/mysqli_driver.php

The member function is random. Row(), Query(), real_escape_string(), num_rows(), etc.

I added a log function to mysqli_driver.php that dumps a stack trace whenever it receives a non-object:

if(!is_object($this->conn_id))
// Dump stack

This tells me that the error is happening in random places in my app so I am assuming the problem is not in my code. That leaves my server configuration as the suspect. I can't find any smoking guns and I cannot reproduce the error.

I'm grasping at straws here. Can anyone offer ideas on what else I can do to track this down?

  • CodeIgniter v3.x
  • Apache/2.2.15
  • CentOS 6
  • MySQL 5.1.73

UPDATE: Here are the configs I use. Main DB connection:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'cfMaster',
    'password' => 'somemasterpw',
    'database' => 'cfMaster',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => FALSE
);

When connecting to an alternate DB:

// $userDb array contains all of the connection details.
$firmDb = array( "firm_id" => $userDb[0]->firm_id, 
"hostname" => $userDb[0]->hostname, 
"username" => $userDb[0]->username, 
"password" => $userDb[0]->password, 
"database" => $userDb[0]->database, 
"dbdriver" => "mysqli", 
"pconnect" => FALSE );
$this->db = $this->load->database($firmDb, TRUE, TRUE);

~35K connections per hour. ~20 failures per day. All of my database connections work fine literally 99.99% of the time. Finding that 0.01% failure is my issue.

"non-object" means that the variable ($db) doesn't refer to an object. Did you assign it to your database class (eg, $db = new databaseClass();, or whatever the class you're using is named)?

Replace

$var = &new class(); with $var = new class();

it will work

try to load database on your method and try

function __construct(){
    parent::__construct();
    $this->load->database();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM