0

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?

2
  • Can you connect to the database using the MySQL command line tool? Commented Apr 21, 2011 at 12:40
  • 3
    is there a database in your mysql named "myblog"? And is your mysql root user password is actually "root"?? Please check it once again. Linux/Unix systems are case-sensitive for database names also... Commented Apr 21, 2011 at 18:26

2 Answers 2

2

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_',
        ),
    ),
  ......
);
Sign up to request clarification or add additional context in comments.

Comments

1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.