简体   繁体   中英

I want to connect YII framework with two databases but it throws CDbConnection failed Incorrect database name

I have used this tutorial to connect YII with two database

I have added a connection string in the protected/config/main.php which is as follows

'db2'=>array(
            'connectionString' => 'mysql:host=localhost;port=3306;dbname=db_name ',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'class' => 'CDbConnection'
    ),

I have created a file under

protected/component/AltActiveRecord.php a class file and the code in this file is

`

abstract class AltActiveRecord extends CActiveRecord
    {
        const BELONGS_TO='CBelongsToRelation';
        const HAS_ONE='CHasOneRelation';
        const HAS_MANY='CHasManyRelation';
        const MANY_MANY='CManyManyRelation';
        const STAT='CStatRelation';     
        public static $db2;

        private static $_models=array();            

        private $_md;                               
        private $_new=false;                        

        private $_attributes=array();               

        private $_related=array();                  

        private $_c;                                

        private $_pk;                               

        public function getDbConnection()
        {
            if(self::$db2!==null)
                return self::$db2;
            else
            {


                self::$db2 = new CDbConnection();
                foreach(Yii::app()->db2 as $key => $value)
                    self::$db2->$key = $value;



                if(self::$db2 instanceof CDbConnection)
                {
                self::$db2->setActive(true);
                return self::$db2;
            }
            else
                throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
            }
            }
            }
`

I have created a model which extends to this class and tried to create the object of this model in controller. but it ends with an error, which says

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [1102] Incorrect database name "db_name"

I am wondering why this is not working. Is it related to php version or yii version

On your connection string, there is an extra space at the end of the line (after db_name ):

'db2'=>array(
            'connectionString' => 'mysql:host=localhost;port=3306;dbname=db_name ',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'class' => 'CDbConnection'
    ),

should be:

'db2'=>array(
            'connectionString' => 'mysql:host=localhost;port=3306;dbname=db_name',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'class' => 'CDbConnection'
    ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM