CodeIgniter dbutil - trouble creating DB
Trying to make a setup for my application. However, I am having trouble getting it to create the database for me.
If I create the database manually, everything is fine.
If the database is not there, I can't do anything, and I get the following error:开发者_JAVA技巧A Database Error Occurred Unable to select the specified database: my_db
Filename: core/Loader.php
Line Number: 232
I'm following the dbutil guide.
My code: function index()
{
$db_exists = FALSE;
$this->load->dbutil();
if( $this->dbutil->database_exists( 'my_db' ) ){
$db_exists = TRUE;
}
}
As per the guide, I get my database driver running in application/config/autoload.php
$autoload['libraries'] = array( 'database', 'datamapper' );
Is your database already specified in the database.php config file?
What you will likely need to do is set your connection settings and leave the database name blank
$db['default']['database'] = '';
Then you can still autoload the database class and then load the dbutil class to check it. After checking and/or creating you would need to set the database name back in the database.php config.
Alternatively, you could remove the database class from autoload and load it on each controller needed. Then, in your installer controller you can load it without the database name using either a DSN or $config array according to the docs: http://www.codeignitor.com/user_guide/database/connecting.html
function index()
{
$dsn = 'mysql://myuser:mypass@localhost';
$this->load->database($dsn);
$this->load->dbutil();
$db_exists = FALSE;
$this->load->dbutil();
if( $this->dbutil->database_exists( 'my_db' ) ){
$db_exists = TRUE;
}
}
精彩评论