note 122028 deleted from pdo.construct by cmb

From: Date: Mon, 28 Sep 2020 13:27:08 +0000
Subject: note 122028 deleted from pdo.construct by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215477@lists.php.net to get a copy of this message
Note Submitter: infatu8 at 163 dot com ---- 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);

« previous php.notes (#215477) next »