Codeigniter showing error: No database selected - mysql

I am using Codeigniter's DBForge class to create a database and table within that database.
Here is code:
if ($this->dbforge->create_database('new_db')) {
$fields = array(
'blog_id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'blog_author' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'default' => 'King of Town',
),
'blog_description' => array(
'type' => 'TEXT',
'null' => TRUE,
),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('blog_id', TRUE);
$this->dbforge->create_table('blog', TRUE);
}
The above mentioned code is written inside index function of controller. When the following code is executed, it shows error as, No database selected. Does anyone have ant idea why it is happening. Any help is appreciated.

You need to specify
$db['default']['database'] = "new_db";
in database.php

Just refer to database.php file inside application/config folder. Make sure you have proper values for database name and other things in the following lines present in database.php file
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'your username', //By default it is root
'password' => 'your password', //By default this field is empty
'database' => 'database name',//The error that you are getting is because of this only. Specify your database name here
'dbdriver' => 'mysqli',
Hope it helps

If you have set the default database in config/database.php and it is still not working, you probably should save the file in case you forget to do that.

Related

cakephp + phpunit + gitlab continuous integration

I'm looking for help with my tests in cakephp + phpunit + gitlab-ci.
I defined my test/fixtures like this: public $import = ['model' => 'MyModel']; to avoid having to redefine the tables schema in the fixture, and have out of sync/wrong code.
I understand that what it does is to look for the live db and use that information to generate the tables in the test db, but what about if there is no live db?
I'm trying to automate my testing in gitlab / continuous integration environment, and there they provide you a mysql container with a single db to run your tests. If I have a single DB, i will not be able to have both a main connection and the testing db, right?
I was thinking on having a create-db-for-tests.sql and imprort that script BUT I understand that running a test with a fixture will remove the table, so I would need to create it again and again on every test.
I could not use the gitlab mysql service and install mysql in the testing container to have 2 databases, like in dev, but I would need to populate the other DB with some script too, and that's the same as defining the db structure in the fixtures.
How can I solve this problem?
Any help will be gladly appreciated.
Something came out from my mind after I read your question.
If you don't have live db, there might be two possibility which is
you may have staging db or your application don't have DB.
According to your second question related about If I have single DB, I would say No. Because, you could have two connections.
e.g
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'foge',
'prefix' => '',
'encoding' => 'utf8mb4',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'test_foge',
'prefix' => '',
'encoding' => 'utf8mb4',
);
When creating a fixture you will mainly define two things: how the table is created (which fields are part of the table), and which records will be initially populated to the table.
I think, you can populate your script with this. Because this is mysql command. But, say honestly i haven't try it before.
mysql -u jenkins -pcakephp_jenkins -e 'DROP DATABASE IF EXISTS test_foge; CREATE DATABASE test_foge; CREATE TABLE users; CREATE Table posts';
And the last one is you can populate data before you run it and I guess you already knew it.
class ArticleFixture extends CakeTestFixture {
public $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'title' => array('type' => 'string', 'length' => 255, 'null' => false),
'body' => 'text',
'published' => array('type' => 'integer', 'default' => '0', 'null' => false),
'created' => 'datetime',
'updated' => 'datetime'
);
public function init() {
$this->records = array(
array(
'id' => 1,
'title' => 'First Article',
'body' => 'First Article Body',
'published' => '1',
'created' => date('Y-m-d H:i:s'),
'updated' => date('Y-m-d H:i:s'),
),
);
parent::init();
}
}
As my personal perspective, good idea to drop and re-create the database before each build as well. This insulates you from chained failures, where one broken build causes others to fail.

creating a database with username and password credentials codeigniter

So i am new to codeigniter, what i want to accomplish is to create a new database using the dbforge class by providing the parameters such as the name of the database, password and the username of this new created database.
so far i have been able to create the database, but i want the username and password to be specified for this database too. How do i accomplish this.
so far i have this function
public function dbcreate($db, $user, $pwd)
{
$this->dbforge->create_database($db);
}
how do i go about incorporating the $user and $password within the newly created database.
$fields = array(
'id' => array(
'type' => 'INT',
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'username' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'password' => array(
'type' =>'VARCHAR',
'constraint' => '100',
),
);
$this->dbforge->add_field($fields);
$this->dbforge->create_table('users', TRUE);

How to Get values for DB configuration from some database Tables in CakePHP ?

I am making an application that will be installed at multiple clients , and DB configuration for every client will be different , as i will be using multiple different databases LIKE Oracle and MySql.
One database will be common in all , i have made a table in same where i will save the db config details , now how to pick that data from the table at database.php .
Can't find anything bit confused .`class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'hrportal_imp',
'prefix' => '',
//'encoding' => 'utf8',
);
//want to fetch data from x table from Default datasource.
public $ora = array(
'datasource' => 'Database/Oracle',
'persistent' => false,
'host' => '<IP i get from above db>',
'port' => '1521',
'login' => '<Data i get from above db>',
'password' => '<Data i get from above db>',
'database' => '<IP i get from above db>:1521/orcl',
'prefix' => '',
'sid' => 'orcl'
);
}`
I did it by Placing the same in Beforefilter of AppController .
App::import('Model', 'ConnectionManager');
ConnectionManager::create('ora',
$config = array('datasource' => 'Database/Oracle',
'persistent' => false,
'host' => 'dynamic Host',
'port' => '1521',
'login' => 'HCM',
'password' => 'hdhd',
'database' => 'dynamic host:1521/dhdh',
'prefix' => '',
'sid' => 'orcl')
);
by default, cakephp will use "default" configurations that you can write within your $default variable. you can also change your database connection as per requirements too. you can check your current selected database on your controller too.
App::import('Model', 'ConnectionManager');
$ds = ConnectionManager::getDataSource('default');
echo $ds->config['database'];

Can i use mysql with yii framework?

I have xampp installed which includes mysql. Can i use this mysql with yii framework?
If yes how can i do it with phpmyadmin. Pls help. Am a newbie..
I have seen many people using sqlite with yii in many tutorials..
ok what you need to do is open /protected/config/main.php, and look for the line like this :
),
'db'=>array( // as you can see this db is currently in use
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
*/
just comment the first connection to sqlite data base and uncomment the second part that uses mysql database :
),
/*'db'=>array(
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),*/
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
that's all!

Codeigniter Migrations + Multiple Tables + One Migration File Per Table

I want to begin using DBForge and migrations class thats built into CI but im not sure how to go about creating migrations for all of my tables.
My thoughts are in my installer process to have a migration file for each of the following tables: advertisements, announcements, config, users, points.. When the user is installing the app, it will automatically run through these migration files and create the tables.
IE: 001_advertisements, 001_announcements, 001_config, 001_users, 001_points
001_map_advertisements
class Migration_map_advertisements extends CI_Migration {
public function up(){
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'youtube_id' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'status' => array(
'type' => 'int',
'constraint' => 11,
'null' => FALSE,
'default' => 1
),
'timestamp' => array(
'type' => 'int',
'constraint' => 11
),
'type' => array(
'type' => 'VARCHAR',
'default' => 'video'
),
'filename' => array(
'type' => 'VARCHAR',
'constraint' => '255'
),
'url' => array(
'type' => 'varchar',
'constraint' => '255'
),
'description' => array(
'type' => 'varchar',
'constraint' => 64
),
'title' => array(
'type' => 'varchar',
'constraint' => 64
)
));
$this->dbforge->create_table('map_advertisements', TRUE);
}
public function down()
{
$this->dbforge->drop_table('map_advertisements');
}
However if i do this and attempt to run a secured controller:
class Developer extends ADMIN_Controller {
function __construct(){
parent::__construct();
} #end constructor function
public function migrate($index){
$this->load->library('migration');
if ( !$this->migration->version($index) ){
show_error($this->migration->error_string());
}else{
echo 'migrated';
}
}
}
i get an error stating that there are multiple version 1 migrations and it fails the migration process. Is there another way to go about this in CI?
Yes, you can only have one migration file per version. A migration is not table-specific, but more like "what schema changes are needed to go from version x to version x+1 of my app?"
Combine all of your existing migration files into 1, and name it something like 001_initial_schema.php. Then, when add a new feature, create a new schema file for that feature. If you have a deployment cycle (like weekly SCRUM sprints), it's nice to have one of these migration files per deployment.
According to http://www.codeigniter.com/user_guide/libraries/migration.html, the best way to "version" your migrations is using the YYYYMMDDHHMMSS format. This should effectively overcome versioning issues as expressed. It is also suggested that if the the filename was "201507071208_advertisements.php" that the contained migration class should be called "Migration_Advertisements".