Hi i’m making a cms for my business and i need a little help trying to get my error exceptions outside of the class
here is an example of the database connection
‘’’
class siteDbh {
private $host = '';
private $user = '';
private $pass = '';
private $dbname = '';
private $charset = '';
protected function connect() {
try {
// Get the variable values used to connect and login
$dsn = 'mysql:host='.$this->host.';dbname='.$this->dbname.';charset='.$this->charset;
// Attempt to connect to the SQL database using PDO(Prepared Database Object)
// Send the Database name, user and pass to the PDO class to try and connect
$pdo = new PDO($dsn, $this->user, $this->pass);
// Pre set attributes to use when making database querys
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (PDOException $e) {
die($e->getMessage());
}
}
}
‘’’
And then i have class’s for each part of my code with functions to SELECT, UPDATE, DELETE etc
So here is an example of one of them functions
‘’’
class siteHTTP extends siteDbh {
// Select HTTP request
public function request() {
try {
$sql = "SELECT http_request FROM settings WHERE 1";
$stmt = $this->connect()->query($sql);
return $stmt->fetch()['http_request'];
} catch (PDOException $e) {
return $e->getMessage();
}
}
}
‘’’
And then i would add these into my main file to be used
‘’’
// Prepared Database Object(PDO) SQL CONNECTION
//////////////////////////////////////////////////////////////////////
require_once INCLUDES_BASEDIR.'_site_pdo/_sitePDO-dbh.class.php';
//#
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Prepared Database Object(PDO) HTTP CLASS
////////////////////////////////////////////
require_once INCLUDES_BASEDIR.'_site_pdo/_sitePDO-http.class.php';
$siteHTTP = new siteHTTP;
////////////////////////////////////////////
if (!$siteHTTP->request()) {
$error[] = $siteHTTP->request();
// Set default request
$http_request = "https://";
} else {
$http_request = $siteHTTP->request();
}
// Get URL Data - Force HTTPS or HTTP
// SITE HTTP Array
define('SITE_HTTP', array(
'request' => $http_request
));
unset($http_request);
‘’’
My problem is “$siteHTTP->request()” is always TRUE. How can i return false and get my exception message when an Exception is caught??
I have an error array inside my main file and i would like to create an if else statement to see if the function is true or false while still getting the exception to put into my $error[].
Any help and advice is realy appreciated. Thank you.