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


RE: Call to a member function real_escape_string() on a non-object - brandotalaguit - 05-11-2018

(09-02-2015, 12:57 PM)Narf Wrote:
(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!

What do you suggest? I got the same problem


RE: Call to a member function real_escape_string() on a non-object - thiago.bittencourt - 05-29-2018

I'm having the same problem and the "autoinit = TRUE" didn't solve it.

I'm facing this error in my model function:

An uncaught Exception was encountered

Type: Error
Message: Call to a member function real_escape_string() on boolean
Filename: /var/www/html/project/system/database/drivers/mysqli/mysqli_driver.php
Line Number: 391

Backtrace:
File: /var/www/html/project/application/models/FeedCompanies.php
Line: 125
Function: where

This is the function:

public function getCompanyMessages($idCompany)
{
$usersModel = $this->modelslibrary->getModelUsers();
$companiesModel = $this->modelslibrary->getModelCompanies();

$where = array(
$usersModel->table_name.'.id_company' => $idCompany,
$this->table_name.'.deleted' => 'N'
);

$this->db->select(
$this->table_name.'.'.$this->primary_key.', '.
$this->table_name.'.token, '.
$usersModel->table_name.'.id_company, '.
$usersModel->table_name.'.slug, '.
$this->table_name.'.id_user, '.
$this->table_name.'.message, '.
$this->table_name.'.posted_as_company, '.
$this->table_name.'.likes, '.
$this->table_name.'.created_at,
getUserName('.$usersModel->table_name.'.name, '.$usersModel->table_name.'.last_name) as name,
getProfilePhotoURL('.$usersModel->table_name.'.profile_photo) as profile_photo,
getCompanyLogo('.$companiesModel->table_name.'.logo) as company_logo, '.
$companiesModel->table_name.'.name as company_name'
);

$this->db->join($usersModel->table_name, $usersModel->table_name.'.'.$usersModel->primary_key.' = '.$this->table_name.'.id_user');
$this->db->join($companiesModel->table_name, $companiesModel->table_name.'.'.$companiesModel->primary_key.' = '.$usersModel- >table_name.'.id_company');
$this->db->where($where);
$this->db->order_by($this->table_name.'.created_at ASC');

$queryResult = $this->db->get($this->table_name);

if (!$queryResult) return null;

$messages = $queryResult->result_array();

if (!$messages) return null;

return $messages;
}

The line 125 is $this->db->where($where);, so I wrote an echo(var_export($where, true)); before this to get the content of $where array, and the content is just this:

array (
'users.id_company' => 2,
'feed_companies.deleted' => 'N',
)

Why am I having this error and how to solve it?

My environment is:

- CodeIgniter 3.0.4
- PHP 7.2.5
- Ubuntu 16.04
- MySQL Server 5.7.22
- Apache 2.4.18

And here is my config/database.php:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => '{{user}}',
'password' => '{{password}}',
'database' => '{{database}}',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'autoinit' => TRUE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);


RE: Call to a member function real_escape_string() on a non-object - ezequielprieto - 09-12-2018

Hi to all!

I'm getting randomly the same error since we upgrade CI to 3.1.9

We tried with autoinit = true, but it doesn't solve the issue.

Any idea how to solve it?


RE: Call to a member function real_escape_string() on a non-object - skm.mas_gmail.com - 01-26-2019

Still, this error continues.


RE: Call to a member function real_escape_string() on a non-object - edures - 06-10-2019

I have fixed it in the following way:

The problem is given because when the update of the cookie in the database is made, the connection with the database has been closed.
My solution is this:

File system / database / DB_driver.php:

Original method:

public function escape_str($str, $like = FALSE)
{
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = $this->escape_str($val, $like);
}

return $str;
}

$str = $this->_escape_str($str);

// escape LIKE condition wildcards
if ($like === TRUE)
{
return str_replace(
array($this->_like_escape_chr, '%', '_'),
array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
$str
);
}

return $str;
}

Modifications of the method:

public function escape_str($str, $like = FALSE)
{
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = $this->escape_str($val, $like);
}

return $str;
}


if($this->conn_id === FALSE){
$this->initialize();
}

$str = $this->_escape_str($str);

// escape LIKE condition wildcards
if ($like === TRUE)
{
return str_replace(
array($this->_like_escape_chr, '%', '_'),
array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
$str
);
}

return $str;
}

the only thing that checks if the connection has been closed, I reopen it.


RE: Call to a member function real_escape_string() on a non-object - InsiteFX - 06-10-2019

And you should never ever edit a CodeIgniter System file NEVER!


RE: Call to a member function real_escape_string() on a non-object - edures - 06-10-2019

(06-10-2019, 03:37 PM)InsiteFX Wrote: And you should never ever edit a CodeIgniter System file NEVER!

jajajajajaja and who solves the error. NO ONE! NO ONE!
look for the failure in stackoverflow, github, this forum and nobody came up with the solution, the sessions expired and they asked again loguin.
publish it in case someone has served as a temporary solution.
We all know the risk that is run by touching the system files.
Greetings.