【发布时间】:2016-04-15 07:37:29
【问题描述】:
我正在使用以下类来创建和访问 PDO 数据库连接:
class DBCxn {
// What Data Source Name to connect to?
public static $dsn='mysql:host=localhost;dbname=dbname';
public static $user = 'root';
public static $pass = 'root';
public static $driverOpts = null;
// Internal variable to hold the connection
private static $db;
// no cloning or instantiating allowed
final private function __construct() {}
final private function __clone() {}
public static function get() {
// Connect if not allready connected
if (is_null(self::$db)) {
self::$db = new PDO(self::$dsn, self::$user, self::$pass, self::$driverOpts);
}
// Return the connection
return self::$db;
}
}
当我尝试以以下方式访问它并且提供的查询失败(测试而不是测试)时,它不会抛出异常:
$db = DBCxn::get();
try {
foreach($db->query('SELECT * from tes') as $row) {
print_r($row);
}
$db = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
代码返回警告:为 foreach() 提供的参数无效
为什么没有捕获到异常?
【问题讨论】: