note 117697 deleted from class.pdo by cmb

From: Date: Fri, 17 Apr 2020 09:35:46 +0000
Subject: note 117697 deleted from class.pdo by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-214490@lists.php.net to get a copy of this message
Note Submitter: sideshowAnthony at googlemail dot com ---- Here is a cheeky PDO helper class to get you started . . . define('DB_MAIN', 'localhost|user1|pa55word|db1'); // Connect to database db1 $db = new my_db(DB_MAIN); // Request "SELECT * FROM table1 WHERE a=16 AND b=22" // Get an array of stdClass's $rows = $db->fetchAll('SELECT * FROM table1 WHERE a=? AND b=?', 16, 22); class my_db{ private static $databases; private $connection; public function __construct($connDetails){ if(!is_object(self::$databases[$connDetails])){ list($host, $user, $pass, $dbname) = explode('|', $connDetails); $dsn = "mysql:host=$host;dbname=$dbname"; self::$databases[$connDetails] = new PDO($dsn, $user, $pass); } $this->connection = self::$databases[$connDetails]; } public function fetchAll($sql){ $args = func_get_args(); array_shift($args); $statement = $this->connection->prepare($sql); $statement->execute($args); return $statement->fetchAll(PDO::FETCH_OBJ); } }

« previous php.notes (#214490) next »