Code: Select all
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;
}
}
Code: Select all
$db = DBCxn::get();
try {
foreach($db->query('SELECT * from tes') as $row) {
print_r($row);
}
$db = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "
";
die();
}
Warum wird die Ausnahme nicht abgefangen?
Mobile version