Master Yoda
Master Yoda

Reputation: 531

"Connecting string cannot be empty" error when initiating CDbConnection

I tried this to create a database connection but it didn't go well ,

try
    {
        $connection=new CDbConnection();
        $connection->active=true;
        $transaction=$connection->beginTransaction();
    }
    catch(Exception $e)
    {
        echo($e);
    }

i got an error:-

connecting string cannot be empty

Upvotes: 1

Views: 2912

Answers (1)

secretlm
secretlm

Reputation: 2361

From Yii Guide on DAO:

To establish a database connection, create a CDbConnection instance and activate it. A data source name (DSN) is needed to specify the information required to connect to the database. A username and password may also be needed to establish the connection.

An exception will be raised in case an error occurs during establishing the connection (e.g. bad DSN or invalid username/password).

$connection=new CDbConnection($dsn,$username,$password);
// establish connection. You may try...catch possible exceptions
$connection->active=true;
......
$connection->active=false;  // close connection

You passed nothing for $dsn so it throw the exception:

CDbException(Yii::t('yii','CDbConnection.connectionString cannot be empty.'));

Upvotes: 1

Related Questions