2016-05-12 1 views
0
<?php 

class MyConnection { 

    protected $username = 'root'; 
    protected $password = ''; 
    protected $host = 'localhost'; 
    protected $db_name = 'testdbewewewewe'; 

    public function connection() { 
     try { 

      $this->dbh = new PDO("mysql:host=". $this->host . ";dbname=".  $this->db_name, $this->username, $this->password); 


     } catch (PDOException $e) {$e->getMessage();} 
    } 


} 



?> 

こんにちは私は誰かがこの1つ私の好奇心をasnwerすることができます、私はPDOでphpを使用してmysqlで接続しようとするが、私はすでにこのコードを作るブラウザは、いいですが、意図的にデータベース名を間違えてみると、ブラウザに何のエラーも表示されませんでした。私が本当に正しく行っているのか不思議です。なぜPDOExceptionがブラウザにエラーを表示しませんでしたか?

+0

PHPでのエラー報告について学んでください。サーバー設定(php.ini)でオフになっていると、エラーは表示されませんが、重大なエラーが発生すると空白のページが表示されます。しかし、以下のバディの答えに従えば、警告や例外をPHPファイルに入れることができますが、重大なエラーや構文エラーはありません。 – Bsienn

答えて

1

あなたのエラー報告が間違っているからです。

You should not catch error exceptions to show them in the browser - PHPはすでにあなたのためにそれを行うことができます。

ので、

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

$conn = new MyConnection(); 
$conn->connection(); 

、ブラウザのエラーを表示するためにPHPを伝え、あなたの例外は最初のクラスを示していそして、このよう

class MyConnection { 

    protected $username = 'root'; 
    protected $password = ''; 
    protected $host = 'localhost'; 
    protected $db_name = 'testdbewewewewe'; 

    public function connection() { 
      $this->dbh = new PDO("mysql:host=". $this->host . ";dbname=".  $this->db_name, $this->username, $this->password); 
    } 
} 

あなたのクラスを作ります。

関連する問題