Database connection error in Yii
I'm going through the Yii tutorial on creating a blog and following the steps. For the database I just imported the test database. Everything was fine.
When I made connection with the database in main.php it is showing the following error:
CDbConnection failed to open the DB connection: SQLSTATE[42000] [1049] Unknown database 'myblog'
The database connection array in main.php is:
//'db'=>array(
// 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
//),
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=myblog',
'emulatePrepare' => true,
'username' => 'root',
'password' =>开发者_开发知识库; 'root',
'charset' => 'utf8',
'tablePrefix' => 'ia_',
),
What am I doing wrong?
Are you following this Yii Blog tutorial?
http://www.yiiframework.com/doc/blog/1.1/en/prototype.database
Are you using MySQL database or just SQLite? The DBName is blog, not myblog on this page.
Tip: If you want to use MySQL instead of SQLite to store data, you may create a MySQL database named blog using the SQL statements in /wwwroot/yii/demos/blog/protected/data/schema.mysql.sql. Then, modify the application configuration as follows,
return array(
......
'components'=>array(
......
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
),
......
);
note to the following error while there is your database! SQLSTATE[42000] [1049] Unknown database ' test :host=localhost
there is a problem in php code
Wrong code
$pdo = new PDO('mysql:host=localhost:dbname=shenakht', "mehdi", "1");
Correct code
$pdo = new PDO('mysql:host=localhost;dbname=shenakht', "mehdi", "1");
The difference between is : and ;
if your code is correct go to the http://localhost/phpmyadmin and create database!
精彩评论