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
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 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
);
I downloaded a project work and installed it in localhost which is done in codeigniter. But when I installed the files and tried to view the website, it is showing the following error:
Fatal error: Unknown: Failed opening required 'C:/wamp/www/website/index.php' (include_path='.;C:\php\pear') in Unknown on line 0
I tried many solutions like changed the include_path settings in php.ini and gave the path but still not working. Can anyone suggest a solution for this ? I have changed the database and config settings in the config folder.
first you have to create dummy database in mysql server ex:MyExampleDb
then go to application->config->database.php
in there code should like this..change database to MyExampleDb and give username in your mysql server and password that's all..hope this works for you if you need more info contact me can help you via teamviewer
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'MyExampleDb',
'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
);
Severity: 8192
Message: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
Filename: mysql/mysql_driver.php
Line Number: 136
Backtrace:
File: C:\wamp\www\ci\application\controllers\c_testing.php
Line: 27
Function: __construct
File: C:\wamp\www\ci\index.php
Line: 315
Function: require_once
in my codeigniter file in databease.php i use this
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost:81',
'username' => 'root',
'password' => '1234',
'database' => 'testing',
'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
);
then in my testing.php file
Class Testingdata extends CI_Model{
function __construct(){
parent::__construct();
$this->load->database();
$this->db->reconnect();
}
// data obtained from database
function get_data(){
$this->db->select('*');
$this->db->from('lecturer_profile');
$query = $this->db->get();
return $query->result();
}
}
?>`enter code here`
You need to change the database config. Go to the following path in your project /application/config/database.php find the dbdriver setting and make sure it's mysqli.
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
);