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

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Call to a member function real_escape_string() on null (/showthread.php?tid=70776)



Call to a member function real_escape_string() on null - thiago.bittencourt - 05-29-2018

I'm facing this error in my model function:
Code:
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:

PHP Code:
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 

PHP Code:
$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:
Code:
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:

PHP Code:
$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_esp - php_rocs - 05-29-2018

@thiago.bittencourt,

Why don't you have CI print the query so that you can see it. This way you can see what generated query is being run.
Here is the link: (The function that you need is $this->db->get_compiled_select()) https://www.codeigniter.com/userguide3/database/query_builder.html?highlight=get_compiled_select#selecting-data


RE: Call to a member function real_esp - thiago.bittencourt - 05-30-2018

(05-29-2018, 12:36 PM)php_rocs Wrote: @thiago.bittencourt,

Why don't you have CI print the query so that you can see it.  This way you can see what generated query is being run.
Here is the link: (The function that you need is $this->db->get_compiled_select()) https://www.codeigniter.com/userguide3/database/query_builder.html?highlight=get_compiled_select#selecting-data

I did it, and I got the query and ran in my MySQL editor, and it worked.

So, to workaround this, I got the full query returned by $this->db->get_compiled_select() and ran into $this->db->query():

PHP Code:
$query $this->db->get_compiled_select();

$queryResult $this->db->query($query); 

And now it's working.