0

I am new with PDO and i tried connecting to the database using TRY and CATCH error handling. But the problem is when i intentionally put some errors in my script, the error message is not showing.

connectionDAO.php :

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

class connectionDAO {

    $username = "root";
    $password = "";
    $host = "localhost";
    $database = "RMSs";
    $dbh = null;

    public function openConnection() {
        $this->dbh = new PDO("mysql:host=". $this->host . ";dbname=" . $this->database, $this->username, $this->password);
        $this->dbh = setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    public function closeConnection() {
        try {
            $this->dbh = null;
        } catch(Exception $e) {
            $e->getMessage();
        }
    }
}

?>

In the above code, my database name is "RMS" but i intentionally input it wrong. When i go to it's URL(localhost/website/php/connectionDAO.php) it's not showing any errors. what seems to be the problem here? My main goal for this is to test either the database if connecting properly before proceeding. Can somebody also give me some other example except mine how to test for database connection in PDO?

Mr Veloso
  • 1
  • 3

0 Answers0