Welcome Guest, Not a member yet? Register   Sign In
Call to a member function real_escape_string() on a non-object
#1

When I use the load->database with the seccond param TRUE, I get the error
Code:
Severity: Error

Message: Call to a member function real_escape_string() on a non-object

Filename: mysqli/mysqli_driver.php

Line Number: 321

This happened after upgrade from codeigniter 2 to 3. I cannot find the problem in the upgrading guide.

The code I use is:
PHP Code:
$ci =& get_instance();
$this->userdb $ci->load->database('default'TRUE);

$qres $this->userdb->get($this->table_name); 
Reply
#2

Hello bboom,

Where is located the code you provided ? In a controller, a library, helper, core extension, ... ?
Reply
#3

This code is located in the constructor of the model.
Reply
#4

(12-10-2014, 03:01 PM)bboom Wrote: When I use the load->database with the seccond param TRUE, I get the error

Code:
Severity: Error

Message: Call to a member function real_escape_string() on a non-object

Filename: mysqli/mysqli_driver.php

Line Number: 321

This happened after upgrade from codeigniter 2 to 3. I cannot find the problem in the upgrading guide.

The code I use is:

PHP Code:
$ci =& get_instance();
$this->userdb $ci->load->database('default'TRUE);

$qres $this->userdb->get($this->table_name); 

Is 'autoinit' set to TRUE for this database? If not, you must call db_connect() manually before executing any queries.
Reply
#5

That was the problem.

I set autoinit to TRUE and it works.

Thanks!
Reply
#6

I have the same problem although i have the autoinit set.
I needed to do this inside the
Quote:C:\work\Codefi UI\codeigniter\system\database\drivers\mysqli\mysqli_driver.php
to make the error go away.

Code:
// --------------------------------------------------------------------

/**
* Platform-dependant string escape
*
* @param    string
* @return    string
*/
protected function _escape_str($str)
{
    if($this->conn_id)
    {
        return $this->conn_id->real_escape_string($str);
    }
}
Reply
#7

(09-02-2015, 09:47 AM)Mdev Wrote: I have the same problem although i have the autoinit set.
I needed to do this inside the

Quote:C:\work\Codefi UI\codeigniter\system\database\drivers\mysqli\mysqli_driver.php
to make the error go away.


Code:
// --------------------------------------------------------------------

/**
* Platform-dependant string escape
*
* @param    string
* @return    string
*/
protected function _escape_str($str)
{
    if($this->conn_id)
    {
        return $this->conn_id->real_escape_string($str);
    }
}

DO NOT DO THIS! You're making yourself vulnerable to SQL injections!
Reply
#8

I have same problem as today, kinda weird thing, in the database.php, I dont see the autoinit constanst , should i just simply added
autoinit =>TRUE

Many thanks
Reply
#9

In our case this was caused by:

$this->db->close();

Which we added to try to deal with a DDoS attack a few months ago. It didn't help but we left the code in there. Apache didn't seem to care about it but it was an issue when me migrated to Nginx.
Reply
#10

(This post was last modified: 02-01-2018, 02:07 AM by biometrics.)

(10-16-2017, 08:38 AM)ciadvantage Wrote: I have same problem as today, kinda weird thing, in the database.php, I dont see the autoinit constanst , should i just simply added
autoinit =>TRUE

Many thanks

We tried adding it but it made no difference.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB