Codeigniter Manual Database Connection - codeigniter

I am doing php in Codeigniter framework.Always Codeigniter support default persistent connections.I don't want to use that connection.I need to connect manually.Is it possible in Codeigniter? If anybody know please help me to go forward.I need little bit explanation also please.

if you want not persistent connection, set up the config file.
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
You can read more in http://codeigniter.com/user_guide/database/connecting.html

You have to update the file in application/config/databse.php
The values depend on the specific environment.For example, when using SQLite you will not need to supply a username or password, and the database name will be the path to your database file.
If you are using xampp server keep the password field blank.
$active_group ='default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test',
'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
);

Related

mysqli::real_connect(): (HY000/1045): Access denied for user [duplicate]

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.

Switch database dynamically in Codeigniter 3.x

In a required scenario, I want to switch to OTHER database dynamically depending on the domain name. I have database-1 in which there is table-1 which stores the hostname, dbname, username and password of the other databases that I need to select and connect dynamically.
I have enabled the 'database' library in autoload.php with the credentials of database-1 in database.php so that when the controller is called, it will check if the OTHER database credentials are already available (in session or some other safezone). If yes, then models will use the already available OTHER database credentials, otherwise SELECT query should be fired on database-1 to fetch the OTHER database credentials and then the models should use the OTHER database credentials.
I have two issues in my implementation:
I don't know the condition where to store and check OTHER database credentials availability. If I save it in a session, OTHER db credentials might be hacked by session hijacking. Also the credentials will be fetched again and again for every visitor of a domain.
CI does not reconfigure the 'database' library or 'models' to work on fetched OTHER database credentials, it still uses the autoloaded database-1 credentials from database.php.
$this->load->model('otherdb');
$otherconfig=$this->otherdb->getotherdb(base_url());
$config=array();
if(isset($otherconfig) && !empty($otherconfig)){
$config['hostname'] = $otherconfig->host;
$config['username'] = $otherconfig->user;
$config['password'] = $otherconfig->pass;
$config['database'] = $otherconfig->nameofdb;
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
}
$this->load->database($config); //doesnt work, still uses old DB
$this->load->model('model1','',$config); //doesnt work, still uses old DB
$this->load->model("model2",'',$config); //doesnt work, still uses old DB
$this->load->model('model3','',$config); //doesnt work, still uses old DB
Connect to the database in your Model rather than in the controller, below approach allows you to use 2 database at the same time. It also helps to keep the database stuff in the model and the controller clear of it.
in your controller, remove the config settings and just load the model like:
$this->load->model('Model1');
$this->Model1->getdata(); // call a function in your model
then in your model you do the db configuration
class Model1 extends CI_Model {
function __construct()
{
parent::__construct();
$otherconfig=$this->getotherdb(base_url());
$config=array();
if(isset($otherconfig) && !empty($otherconfig)){
$config['hostname'] = $otherconfig->host;
$config['username'] = $otherconfig->user;
$config['password'] = $otherconfig->pass;
$config['database'] = $otherconfig->nameofdb;
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$this->db2=$this->load->database($config, true);
$this->db=$this->load->database('', true); //load the 'default' database as defined in your config/database.php
}
function getdata()
{
$db2->query();
$db2->result();
}
function getotherdb($base_url)
{
// get your database credentials
// you can still use the autoloaded db
$db->query();
$db->result();
}
}
from the docs: Connecting to Multiple Databases
Depending on what you want to do the application of the logic may be different but with that being said you can configure multiple Database connections in CI in your database.php file.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'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
);
$db['second'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'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
);
To use the second database connection in your controllers you can simply select it using this:
private $db2;
function __construct(){
parent::__construct();
$db2 = $this->load->database('second', TRUE);
// YOU NEED TO ADD TRUE AS THE SECOND PARAMETER TO LOAD THE DB OBJECT
}
public function Users(){
$query = $this->db2->get('TABLE_NAME');
return $query->result_array();
}
If you are loading the database configuration from an initial DB this can still be done, don't forget that codeigniter is still PHP.
The $db[] array can be created dynamically using logic, you could create a json file that stores such information and populate the db array with it.

Codeigniter PDOException: SQLSTATE[HY000] [1049] Unknown database [duplicate]

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.

unable to connect to database using the provided settings my error

i have this project created by php and mysql but i have error
Filename: C:\xampp\htdocs\demo\system\database\DB_driver.php
Line Number: 124
and uploaded my project to solve this with very thanks for all team .
http://www.webchinupload.com/f/2019-05/ebae9eaf2a9655eb442da8a9e648da3e.rar
my error in line 124
$this->conn_id = ($this->pconnect == false) ? $this->db_connect() : $this->db_pconnect();
// No connection resource? Throw an error
if ( ! $this->conn_id)
{
log_message('error', 'Unable to connect to the database');
if ($this->db_debug)
{
$this->display_error('db_unable_to_connect');
}
return FALSE;
}
Change hostname to localhost
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = ''; #database password
$db['default']['database'] = 'e-frosh_test';
Check your application/config/database.php
In codeigniter framework
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',//Put Your db passwaord
'database' => 'e-frosh_test',
'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
);

About database query

I am developing a application in Codeigniter Framework and I have been created a model with name "Surveyor_model" and I am trying to execute query but it is not executing and give a message "table or view does not exist."
I have been create a model in codeigniter with name " Surveyor_model"
and I am trying to execute a query with syntax
$this->oracle_db->get('users_surveyor');
But it is showing a query as given below
select * from "users_surveyor"
and showing a error message "Table or view does not exist"
I want to remove double quotation from a query as given below
select * from "users_surveyor"
so please tell me how can I remove double quotation.
And my I am also showing a database config and please see as given below
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.9.1.217)(PORT = 1521)) (CONNECT_DATA = (SERVICE_NAME = orcl) (SID = orcl)))";
$active_group = 'oracle';
$query_builder = TRUE;
$db['oracle'] = array(
'dsn' => '',
'hostname' => $dbtns,
//'username' => 'igistest',
//'password' => 'igistest',
'username' => 'cwms',
'password' => 'cwms321',
'database' => 'orcl',
'dbdriver' => 'oci8',
'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
);
Form this post you can use.
SET GLOBAL SQL_MODE=ANSI_QUOTES;
For more information you can read this article Link.

Resources