<?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();}
}
}
?>
嗨我是PDO的新手,我希望有人可以对这个问题感到好奇,我尝试使用php和PDO在mysql中建立连接,但是当我已经制作了这个代码时,浏览器并没有显示任何错误表现不错,但是当我试图故意使数据库名称出错时,浏览器根本没有显示任何错误,我只是好奇我是否真的做得对吗?
答案 0 :(得分:1)
仅因为您的错误报告错误。
You should not catch error exceptions to show them in the browser - 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);
}
}
然后告诉PHP在浏览器中显示错误
error_reporting(E_ALL);
ini_set('display_errors', 1);
$conn = new MyConnection();
$conn->connection();
让您的例外显示在头等舱。