Dip
Dip

Reputation: 363

Issue with postgre database driver in Code Igniter

I am trying to query a table from a postgreSQL database in PHP with CodeIgniter 3.1.9 framework. The issue is I am getting the error

A PHP Error was encountered
Severity: Notice

Message: Undefined property: CI_DB_postgre_driver::$db

Why am I getting this error and how may I resolve it?

What I have done so far. I made sure I was using the right port in which I am, I know 6090 is not the default port but that is what my postgreSQL db is using. I have provided everything needed to connect to postgreSQL in the database.php file. I uncommented both of these from the php.ini file:

extension=php_pgsql.dll

extension=php_pdo_pgsql.dll

$db['database_2'] = array(
'dsn'   => '',
'hostname' => 'example01',
'username' => 'user',
'password' => 'pwrandom',
'database' => 'dbrandom',
'dbdriver' => 'postgre',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
'port' => '8060');

Here are the executed statements. The error occures on the second statement...

$secondDB = $this->load->database('database_2', TRUE);
$secondDB->db->query("SELECT VERSION()")->row('version');

Upvotes: 0

Views: 90

Answers (1)

Vickel
Vickel

Reputation: 7997

I am not working with postgre, but the error comes from ->row('version'). you try to get a row from the query, but don't call it the correct way

try like this:

$secondDB = $this->load->database('database_2', TRUE);
$obj= $this->db->query('SELECT VERSION()');
echo'<pre>';print_r($obj->row()); die();

which outputs in my case: [VERSION()] => 5.6.12-log

You could also have a closer look and print out the object, to see how it's structured:

echo'<pre>';print_r($obj); die();

in my case (mysqli) the object looks like:

CI_DB_mysqli_result Object
(
    [conn_id] => mysqli Object
        (
            [affected_rows] => 1
            [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 40933630edef551dfaca71298a83fad8d03d62d4 $
            [client_version] => 50011
            //some more 
            [server_info] => 5.6.12-log
            //some more
         )
)

Now you change ->row('version') accordingly to this structure:

In my example I would use: echo $obj->conn_id->server_info;die;

which outputs now: 5.6.12-log

more in the docs: Generating Query Results: result rows

Upvotes: 1

Related Questions