CodeIgniter Forums
Call to a member function real_escape_string() on a non-object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: Issues (https://forum.codeigniter.com/forum-19.html)
+--- Thread: Call to a member function real_escape_string() on a non-object (/thread-501.html)

Pages: 1 2


Call to a member function real_escape_string() on a non-object - bboom - 12-10-2014

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); 



RE: Call to a member function real_escape_string() on a non-object - _this - 12-11-2014

Hello bboom,

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


RE: Call to a member function real_escape_string() on a non-object - bboom - 12-11-2014

This code is located in the constructor of the model.


RE: Call to a member function real_escape_string() on a non-object - Narf - 12-11-2014

(12-10-2014, 04: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.


RE: Call to a member function real_escape_string() on a non-object - bboom - 12-11-2014

That was the problem.

I set autoinit to TRUE and it works.

Thanks!


RE: Call to a member function real_escape_string() on a non-object - Mdev - 09-02-2015

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);
    }
}



RE: Call to a member function real_escape_string() on a non-object - Narf - 09-02-2015

(09-02-2015, 10: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!


RE: Call to a member function real_escape_string() on a non-object - ciadvantage - 10-16-2017

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


RE: Call to a member function real_escape_string() on a non-object - biometrics - 01-31-2018

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.


RE: Call to a member function real_escape_string() on a non-object - biometrics - 01-31-2018

(10-16-2017, 09: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.