I'm trying to learn CodeIgniter, but unfortunately many of the tutorials they list on their wiki are several years old and, based on others' comments, don't work on the newest version of CodeIgniter.
I did find one that didn't have any negative comments - on IBM DeveloperWorks- but I can't get it to work. Everything up until the form submit is fine, but after I submit I get a blank page and nothing is sent to the database.
How can I test/troubleshoot a database connection in CodeIgniter? I know my settings (as far as host, dbname, username/password, etc.) are correct because I'm using them successfully with a plain vanilla PHP site.
Edit to add: alternatively, can anyone point me to a recent beginner tutorial that works with the recent version? I don't need an MVC tutorial; I'm familiar with the design pattern. I just need to learn CodeIgniter.
Edit to add database.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = "default";
$active_record = TRUE;
//$db['default']['hostname'] = "localhost";
$db['default']'hostname'] = "myHostName.powwebmysql.com";
$db['default']['username'] = "myUserName";
$db['default']['password'] = "myPassword";
$db['default']['database'] = "codeigniter"; //yes, database is called codeigniter
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
/* End of file database.php */
/* Location: ./system/application/config/database.php */
EDIT: error message when adding database load to constructor:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Welcome::$load
Filename: controllers/welcome.php
Line Number: 6
where line 6 is $this->load->database();
Step 1 : Configure database
application/config/database.php
$db['default'] = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'my_database',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
);
Step 2: Load database In your controller:
$this->load->database();
Step 3: Test the connection
if ($this->db->conn_id) {
echo "Database connected successfully!";
} else {
echo "Database connection failed.";
}
Step 4: Alternative safer method
$this->load->database();
if ($this->db->initialize()) {
echo "Connected to database!";
} else {
echo "Connection error!";
}
Step 5: Test by running a query
$query = $this->db->query("SELECT 1");
if ($query) {
echo "Database is working correctly.";
}
For CodeIgniter 4
$db = \Config\Database::connect();
if ($db->connect()) {
echo "Connected successfully!";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With