0

Afternoon all. I have a strange question. I need to have two connections to our db in codeigniter instance. I need to have the standard ci method but i also need to create a pdo class instance for use in new feature setup. We are unfortunately in the position where we have to run a v1.0 system using the old db connectivity and a new v2.0 system using a pdo db object.

How can I go about creating a global instance of this pdo class so that it only gets instantiated once per process call to codeigniter.

1 Answers1

0

you should add another DB in application/config/database.php and change dbdriver and hostname like

$db['pdodb']['hostname'] = "mysql:host=localhost'";
$db['pdodb']['username'] = "root";
$db['pdodb']['password'] = "";
$db['pdodb']['database'] = "your_db";
$db['pdodb']['dbdriver'] = "pdo";
$db['pdodb']['dbprefix'] = "";
$db['pdodb']['pconnect'] = TRUE;
$db['pdodb']['db_debug'] = FALSE;
$db['pdodb']['cache_on'] = FALSE;
$db['pdodb']['cachedir'] = "";
$db['pdodb']['char_set'] = "utf8";
$db['pdodb']['dbcollat'] = "utf8_general_ci";
$db['pdodb']['swap_pre'] = "";
$db['pdodb']['autoinit'] = TRUE;
$db['pdodb']['stricton'] = FALSE;

and you can load database like this

$pdodb= $this->load->database('pdodb', TRUE);

and finally you can execute query like this

$query = $pdodb->select('col1, col2,col3')->get('your_table');

for more information about multiple database

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • ok but can i call this once in the start of the process and have it available wherever I need to use it? – Terence Bruwer Oct 12 '18 at 11:02
  • when you want to execute query then first you need to database object `$pdodb= $this->load->database('pdodb', TRUE);` this line return you DB object and follow execute query process (above i mention 2 lines of code that help to execute query) – Bilal Ahmed Oct 12 '18 at 11:04