Welcome Guest, Not a member yet? Register   Sign In
How to Check if database connection exists Ci4
#1

I'm trying to check id database connection exist or not then redirect to a page where a user can be able to create a database through form inputs.

Here's what I tried to do but not succeed.

PHP Code:
public function index()
 {
 
// $database = \Config\Database::connect();
 
$database = \CodeIgniter\Database\Config::getConnections();

 if (
$database
 {
 
//db connection initialized
 
return view('dashboard');
 }
 else
 {
 
//db connection  not initialized
 
return redirect('db_setup');
 }
 } 
Reply
#2

(This post was last modified: 07-14-2021, 02:42 AM by InsiteFX.)

These are not tested but one of them should work for you.

PHP Code:
$db = \Config\Database::connect();


/* If an established connection is available, then there's
 * no need to connect and select the database.
 *
 * Depending on the database driver, conn_id can be either
 * boolean TRUE, a resource or an object.
*/
if ($db->initialize()){
    // we have a connection
} else {
    // NO Connections
}

// or try
if ($db->connID){


This is because CodeIgnioters database connection does not connect until a query is ran
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB