I am conneting two databases in codeigniter. My database.php configuration is as follows.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'dvrs',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['orcl_db'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'mvrs',
'password' => 'mvrs',
'database' => 'MVRS',
'dbdriver' => 'oci8',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Now I am autoloading the default database, and loading the orcl_db in the respective model on demand using
$this->orclDB = $this->database->load("orcl_db", TRUE);
I am connecting to both dbs and running queries successfully.
I need to make sure that the oracle server is available before connecting to it and display proper error messages if the server is not available / not responding.
What will be the best way to do this?
Following is the code to ensure it.
$this->orclDB = $this->load->database('orcl_db', TRUE);
if (!$this->orclDB ->initialize()) {
$response["status"] = false;
$response["message"] = "Oracle DB is not available.";
}
In order to turn off db debugging, which throws fatal error when db connection fails, use $db['orcl_db']['db_debug'] = FALSE; in your database config file. Then you can check if database is loaded like this:
if ( $this->load->database('orcl_db') === FALSE )
{
// do whatever you think is appropriate, but do not panick
}
Related
I am hosting a CodeIgniter API on GCP App Engine and I cant connect to the GCP Cloud SQL. I create the instance and database, but when I try to conect via CI I got a timeout message
error screen
My databse config on CI is:
$db['default'] = array(
'dsn' => 'mysql:unix_socket=/instance_name/instancia;dbname=project_name',
'hostname' => 'http://00.000.00.00',//here i put the public ip from the sql instance
'username' => 'user',
'password' => 'passwd',
'database' => 'projeto',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => yes,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I've been banging my head on this for a while, myself, and finally cracked it! Here's what my db config looks like:
$db['default'] = array(
'dsn' => '',
'hostname' => '/cloudsql/PROJECT-NAME:REGION:DATABASE',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'DATABASE_NAME',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
// 'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
The important bits are that the dsn is empty and the hostname is the full path to the unix socket. The rest of it should be about the same.
Hope that helps you, too!
I have an autoloaded DB which has all its var set in the config/database.php file as a default group:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'dbname',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => false,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => false,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => false,
'compress' => false,
'stricton' => false,
'failover' => array(),
'save_queries' => true
);
In this default DB there is a table where I need to read an external DB, and I have all fields to get a new connection.
I need to connect both DB at the same time, but I cannot define the DB variables in the config/database.php as these are dynamic and may change depending on the DB (default) content.
My idea was this either to SET $db['external'] = [...] IN THE CONTROLLER and set the data from the default DB I read, or simply use a DNS:
Solution #1:
public function wordpress()
{
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$db['wp_db'] = array(
'dsn' => '',
'hostname' => $wp_db->mysql_host,
'username' => $wp_db->mysql_user,
'password' => $wp_db->mysql_password,
'database' => $wp_db->mysql_db,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => false,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => false,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => false,
'compress' => false,
'stricton' => false,
'failover' => array(),
'save_queries' => true,
);
$this->WPDB = $this->load->database('wp_db', true);
}
Solution #1 gives "You have specified an invalid database connection group (wp_db) in your config/database.php file." error
Solution #2:
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$wp_dns = "mysql://$wp_db->mysql_user:$wp_db->mysql_password#$wp_db->mysql_host/$wp_db->mysql_db";
$this->WPDB = $this->load->database($wp_dns, true);
Solution #2 gives a "Invalid DB Connection String" error
Ps: I'm moving to Laravel, but this project was built with CI already :)
the only thing you've to change in your function is the following
public function wordpress()
{
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$arrDbData = array(
'dsn' => '',
'hostname' => $wp_db->mysql_host,
'username' => $wp_db->mysql_user,
'password' => $wp_db->mysql_password,
'database' => $wp_db->mysql_db,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => false,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => false,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => false,
'compress' => false,
'stricton' => false,
'failover' => array(),
'save_queries' => true,
);
$this->WPDB = $this->load->database($arrDbData, true);
}
I found out Solution #2 is working but due to the dns connection which is a STRING you are to make sure the password is made of letters and numbers and NO SYMBOLS otherwise it screws up the string and does not read properly.
In my case the password was this one iidf#q0RDTh#)CrPo5PDLeVe so dashes and parenthesis created a problem where CI could not read the whole password.
I'm new to Codeigniter PHP framework. When I'm testing my application I get 'Unknown database db_name' error. I have browsed through several sites but didn't found solution for the problem as I'm trying the same to connect with wamp's mysql database. Any help would be appreciable.
Following is database.php in config folder: image describing Test database:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array()
);
I was getting same error when i installed new wampserver 3.2.0, it is an evil it installs MariaDB by default and port 3306 is assigned to it , you cannot even change it. I think codeigniter/php tries to connect to this port by default.
I used following , it worked for me i.e. hostname => 'localhost:3308'
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost:3308',
'username' => 'root',
'password' => '',
'database' => 'soft',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
That's what i did to handle whether db exists or not :
First Change this in application/config/database.php
'db_debug' => (ENVIRONMENT !== 'production')
to
'db_debug' => FALSE;
Then add these two in your controller
$this->load->dbforge();
$this->load->dbutil();
Then check it in your method
if( $this->dbutil->database_exists($this->db->database))
{
echo 'Database Already Exists';
}
else
{
if($this->dbforge->create_database($this->db->database))
{
echo 'Database created successfully !';
}else
{
print_r($this->db->error());
/*
Array (
[code] => 1007
[message] => Can't create database 'my_db'; database exists
)
*/
}
}
I was too getting this error. There are 2 fields in application/config/database.php file that should match up with your actual database:
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
Make sure, the actual DB is of the same type & char_set as mentioned in the above file.
I always get the following error in my codeigniter 3 application when I start using a sqlite3 database.
SQLite3::query(): Unable to prepare statement: 1, no such table: pages
I have read somewhere that this is the case when you have the wrong path to the databse. My database is located in application/db/pages.sqlite
This is the config for the sqlite database:
$db['pages'] = array(
'dsn' => '',
'hostname' => '',
'username' => '',
'password' => '',
'database' => APPPATH.'db/pages.sqlite',
'dbdriver' => 'sqlite3',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
What have I done wrong?
Try this, it worked for me before
'database' => 'sqlite:'.APPPATH.'db/pages.sqlite',
Also make sure that folder db is readable by your app.
First you need to check that sqlite3 php module is installed on your web server or not?
This will required to work Codeigniter3 sqlite3 db driver.
If its not working then provide 'dsn' string. This will definitely working.
First thing try to check your php.ini and uncomment this
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
and try this
$db['default'] = array(
'dsn' => 'sqlite:application/db/pages.sqlite',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
These are my settings in the database config file for Codeigniter:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'admin',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
And even if I set the debuging to false errors still show on my page specificly after this function:
if ($this->db->update_batch($batch_data, 'id'))
{
echo 'Success';
}
else
{
echo 'Error';
}
I know why this happens (incorrect id value and in this case missing argument) but I don't know why the error is displayed and more importantly why is there no false on return? Instead it returns the "A Database Error Occurred" block!