pdo单例模式
<?php
class PdoExt extends PDO{
static protected $_instance;
protected function __construct($dsn, $username, $password){
return parent::__construct($dsn, $username, $password);
}
static public function getInstance($dsn, $username, $password){
if(!isset(self::$_instance)){
self::$_instance = new self($dsn, $username, $password);
}
return self::$_instance;
}
}
$dsn = "mysql:host=localhost";
$username = "root";
$password = "root";
$pdo = PdoExt::getInstance($dsn, $username, $password);
$sql = "SELECT user,host FROM mysql.user WHERE user = :user";
$statement = $pdo->prepare($sql);
$username = 'root';
$statement->bindParam(':user', $username);
$statement->execute();
$users = $statement->fetchAll(2);
print_r($users);