How to get the MySQL link identifier from Yii

I would like to pass the MySQL link identifier to some other PHP files to be used further on a query.

How can I retrieve it?

I’m not sure whether you are finding this function or not

Yii::app()->db->createCommand();

This can help you to create and run your sql.

I would like to access the MySQL link identifier outside of Yii framework. I can’t relly on that method now.

I use a charting library which needs connection to the database, and therefor I want to pass it to it.

Is it possible?

Take a look at CDbConnection::getPdoInstance() and PDO::__construct()

Thanks.

Can I transform this PDO link to a mysql_query() compatible one?

No, you have to use PDO methods as listed on php.net. It’s a little different compared to mysql_*.

Then I would like to ask something different.

Can I get statically the db host, user, password, db name to pass on?

You mean from the CDbConnection instance? The problem is, PDO (and thus CDbConnection) makes use of a connection string (which contains host and db name).

However, you can access the connection string via CDbConnection::connectionString (also see username & password). Maybe you want to do something like this in your Yii config:




<?php

define('MYSQL_HOST', 'localhost');

define('MYSQL_DATABASE', 'test');

define('MYSQL_USERNAME', 'root');

define('MYSQL_PASSWORD', '');


...


return array(


   'components' => array(

      'db' => array(

         'connectionString' => 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DATABASE,

         'username' => MYSQL_USERNAME,

         'password' => MYSQL_PASSWORD,

      ),

   ),


);


?>



You can now use the defined constants in order to connect via mysql_connect. But why not use the existing pdo instance?