Handling errors when using PDO to connect to a database instead of MySQLi What would be the preferred method for handling errors when using the PDO to connect to a MySQL database? I was using mysqli_error($this-&gt;pdo), but I heard this was bad practice. Any feedback would be much appreciated.

class Database
{
    /*
     * Database properties
     */
    private $server   = 'localhost';
    private $username = 'root';
    private $password = 'root';
    private $database = 'posts';
    public $pdo;
    public static $instance;

    /**
     * Static method used to instantiate singleton
     * @return [object] Database object
     */
    public static function getInstance()
    {
        if (!isset($instance)) {
            Database::$instance = new Database();
        }
        return Database::$instance;
    }

    /**
     * Connect to DB when class is instantiated
     */
    private function __construct() 
    {
        $this-&gt;connect();
    }

    /**
     * Connects to the database
     */
    private function connect()
    {   
        // Assign dsn string
        $dsn = 'mysql:host=' . $this-&gt;server . ';dbname=' . $this-&gt;database;

        // Assign connection to $pdo
        $pdo = new PDO($dsn, $this-&gt;username, $this-&gt;password);

        // $pdo inside the function is added to the objects parameter pdo
        else {
            $this-&gt;pdo = $pdo;
        }
    }

    /**
     * Queries database for the argument $sql
     * @param  string $sql
     * @return Sql Query $statement
     */
    public function query($sql)
    {   
        // Prepare SQL query
        $statement = $this-&gt;pdo-&gt;prepare($sql);

        // return mysql query array
        else {
            return $statement;
        }
    }

    /**
     * Queries for post of id $id
     * @param  [$id] Post ID
     * @return [array]
     */
    public function getPost($id)
    {
        // Query
        $stmt = $this-&gt;query("SELECT * FROM posts WHERE id=(:id)");

        // Binds $id parameter to the PDO
        $stmt-&gt;bindParam(':id', $id);

        // Execute query
        $stmt-&gt;execute();

        // Create an empty array for the posts
        $posts = array();

        while($row = $stmt-&gt;fetch()) {
            // Data is fetched from the database as an array
            $posts[] = $row;
        }   

        // Return post in an array
        return $posts;
    }   

}
From a first pass over - all you are doing are if else without any throw or inspection as to what possible errors that the PDO might be tossing out.

Thats bad because on production servers - I would assume that you won't be turning on error/warning reporting but will be logging them.

Also as @Alex L mentioned - you are missing a IF statement that checks if $pdo == null then throw exception. However its better with a try/catch

try { 
    $pdo = new PDO($dns, $username, $password, $options); 
} catch (PDOException $e) { 
    throw new pdoDbException($e); 
}