I have three classes each handling a different functionality when connecting to my database.
Class 1 with name ConnectDB() which handles MySqli() connection; Class 2 with name MessageOut() which gives error and success messages via a $_SESSION['message'] key to the user UI and Class 3 with name WebApp() uses the MySqli connection to retrieve the data from the database and pass it to the UI.
This differs from how I normally do database connection, collection and display. Normally I just created a lot of functions in three files and called them if I needed them. But my site is getting bigger and more complicated so I am re-organising everything.
My current problem is when I create the db connection within my 3rd class it is not throwing the error I programmed.
class ConnectDB {
//Connecting to database
public function connect() {
//connecting to mysql
@$conn = new mysqli(DB_HOST, DB_USERRW, DB_PASSWRW, DB_DBASE);
// check validity of database connection
if (mysqli_connect_errno()) {
return false;
exit();
}
// select the database to use
return $conn;
}
}
ini_set('display_errors', 1);
ini_set('log_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
class WebApp {
protected $db;
function __construct(){
$this->connector = new ConnectDB();
$this->messager = new MessageOut();
try {
$this->db = $this->connector->connect();
if(!$this->db) {
throw new Exception(' Database connection is currently unavailable.');
}
} catch (Exception $e) {
mysqli_report(MYSQLI_REPORT_OFF);
$errors = array();
$message = $e->getMessage();
//populate array with message and divert output to error class functions
array_push($errors, $message);
$this->messager->erroutput($errors);
}
}
public function selectIdata() {
//select data
try {
$query = "SELECT *
FROM thetable";
$stmt = $this->db->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($idata);
$result = [];
if ($stmt->num_rows > 0) {
while ($stmt->fetch()) {
$result[] = $idata;
}
return $result;
} else {
return false;
}
} catch (Exception $e) {
mysqli_report(MYSQLI_REPORT_OFF);
$errors = array();
$message = $e->getMessage();
//populate array with message and divert output to error class functions
array_push($errors, $message);
$this->messager->erroutput($errors);
}
}
}
I changed my localhost defined password to the wrong one and loaded the file, but the error is given on line 10 within my 1st class even though the error is suppressed. The idea is to check the connection before using it.
How do I get the error thrown with in my 3rd class giving me the 'Database connection is currently unavailable.' message?
Edit
So I re-assessed what I did and set a try-catch block within the constructor of my 3rd class, but know I get an error: Fatal error: Uncaught Error: Call to a member function prepare() on null in the 3rd class on line 41 where the db connection is used.
if(!$this->db)__constructorof theWebApp()class whether the database connection is false, if it is then throw the error that there is no connection available. I have noticed that I have to somehow bubble the exception from theConnectDB()class to theWebApp()class in order to catch it.if(!$this->db)never be true? The property$this->dbis set to the return value of$this->connector->connect()method. If the return value is false then wouldn't!$this->dbbe true i.e.$this->db === false?