Reputation: 694
I have a config.php file which is as following:
//file name: config.php -------------
define ("DOMAIN","mydomain.com");
//connection parameters
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
echo "Database connected successfully!";
} catch (PDOException $e) {
throw new PDOException($e->getMessage());
}
and now I have including this file in the index.php as follows:
//file name: index.php -----------
require_once("../path to/config.php");
echo "WELCOME to <h1 style='display:inline;color:#000080;'>".DOMAIN."</h1>";
As you can see, the credentials are fake, it will not be able to connect to the database, so the error message is showing as below:
Fatal error: Uncaught PDOException: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO) in /home/hosting_account/path to/config.php:20 Stack trace: #0 /home/hosting_account/public_html/index.php(22): require_once() #1 {main} thrown in /home/hosting_account/path to/config.php on line 20
I have removed the actual path here but mentioned the layout of the path. All works normal.. The error message is normal as well.
But what I want, the first line of the index.php will be executing/showing (Welcome to mydomain.com) even there will be an error in a part of (here the connection part) the included file. As there is no error in defining the DOMAIN and echoing it from the index.php, I want it will show the "Welcome to mydomain.com" and then will show the error message caught from the pdo connection.
How to do that?
Upvotes: 0
Views: 50
Reputation: 160
//file name: config.php -------------
define ("DOMAIN","mydomain.com");
//connection parameters
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$pdo_error = 0;
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
echo "Database connected successfully!";
} catch (PDOException $e) {
//throw new PDOException($e->getMessage());
$pdo_error = 1;
$error_message = $e->getMessage();
}
And then
//file name: index.php -----------
require_once("../path to/config.php");
echo "WELCOME to <h1 style='display:inline;color:#000080;'>".DOMAIN."</h1>";
if($pdo_error){
throw new PDOException($error_message);
}
Upvotes: 1
Reputation: 96
You shouldn't display any errors on the production server - you should log it instead. If you are not using any framework you can simply save it to the file instead of displaying it - do all of it in the catch
block.
Besides you are catching and throwing the same exception one more time.
} catch (PDOException $e) {
throw new PDOException($e->getMessage());
}
please edit this part as you with to handle it.
Upvotes: 2