CodeIgniter Forums
[SOLVED] How to test if a database is connected - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: [SOLVED] How to test if a database is connected (/thread-74971.html)



[SOLVED] How to test if a database is connected - John_Betong - 12-02-2019

[font=Tahoma, Verdana, Arial, sans-serif]I am having difficulty in trying to test if database has connected. [/font]

[font=Tahoma, Verdana, Arial, sans-serif]I know that the database does not exist and would like the application to fail gracefully. [/font]

[font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif]When the database does not exist I would like to show download SQL database installation details.[/font][/font][/font][/font]

[font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif]Edit:      [/font][/font][/font][/font]

PHP Code:
//============================================================
private function isConnected
(
  
string $dbName
)
bool
{
  $result FALSE;
    
  error_reporting
(0);  
    $link 
mysqli_connect("localhost""userName""passWord"$dbName);
  error_reporting(-1);

  $result = isset($link->errno) ;
  
  
return $result;   
}//


//============================================================
private function newDatabase
(
  
string $dbName
)
bool
{
  $result FALSE;
  # error_reporting(0);

  $conn   mysqli_connect("localhost""userName""passWord");
  $sql    'CREATE DATABASE ' $dbName ;
  $result $conn->query($sql);

  return $result;
}
//  

//============================================================
private function newTable
(
  
object $conn
  
string $dbName
  
string $table
)
bool
{
  $result FALSE;

  $sql file_get_contents(BASEURL .'assets/sql/' .$table .'.sql');
  $result $conn->query($sql);

  return $result TRUE;
}
//  
[font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif][font=Tahoma, Verdana, Arial, sans-serif]
Next hurdle - create indexii Smile
[/font]
[/font]
[/font]
[/font]