mysqli::real_connect(): (HY000/1049): Unknown database error in codeigniter - php

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.

Related

An Error Was Encountered You have not selected a database type to connect to

$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'walkmjcd_moneywk',
'password' => 'q?P?Lf817H~y',
'database' => 'walkmjcd_walkmoney',
'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
);
$db['default']='';
An Error Was Encountered You have not selected a database type to connect to. Please ANyone help in this case How can i resolve this.
The problem is, the db configuration you set at top and reset it at the bottom. Just remove the $db['default']=''; and try.
Please remove :
$db['default']='';

Codeigniter connect to second DB based on first DB

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.

How to connect odbc database using codeigniter?

$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => 'tes',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => 'tes',
'dbdriver' => 'odbc',
'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
);
When I run the program there is an error:
" Message: Call to undefined method CI_DB_odbc_driver::select() Filename: models/m_city.php"
m_city.php file:
<?php
class m_city extends CI_Model
{
function get_all($where = array())
{
$this->db->select('Name,Population');
$this->db->where($where);
$this->db->limit('50');
$query = $this->db->get('City');
return $query->result_array();
}
}
I already made dsn name on odbc. the name of dsn is "tes".
If you use "Mysql" then follow the following instructions.
Please put database username and database password in the following:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',//Put database username
'password' => '',// put database password
'database' => 'tes',
'dbdriver' => 'odbc',
'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
);
After complete this save the page and open the page 'autoload.php' under the config folder. There search "$autoload['libraries']" option. Put the following
$autoload['libraries'] = array('database');
If you use PDO, PostgreSQL, Oracle then see the following links:
https://www.codeigniter.com/user_guide/database/configuration.html

Codeigniter + Lampstack - Unable to connect to your database

i have this on my localhost and it works perfectly fine.
but when i put it on a server i configured the database settings correctly
here is my config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => '111.111.1.111', //sample host
'username' => 'root',
'password' => 'password',
'database' => 'mydatabase',
'dbdriver' => 'mysqli',
'port' => '1234',
'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' => FALSE
);
but all i got was the error
Unable to connect to your database server using the provided settings.
Filename: third_party/MX/Base.php
Line Number: 55
what is the problem?

Connecting two databases in Codeigniter

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
}

Categories