Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test a database connection with CodeIgniter?

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();

like image 208
EmmyS Avatar asked Feb 14 '26 07:02

EmmyS


1 Answers

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!";
}
like image 186
Prachi Sharma Avatar answered Feb 15 '26 19:02

Prachi Sharma