php[tek] 2017: Atlanta

Ursprüngliche MySQL API

add a note add a note

User Contributed Notes 2 notes

sideshowAnthony at google dot com
1 year ago
Here is a 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){
            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();
        $statement = $this->connection->prepare($sql);       
         return $statement->fetchAll(PDO::FETCH_OBJ);

(Phillipus - I don't know what nationality you are, but the word "moron" is pretty offensive in UK English. Thank you)
development at pureconcepts dot net
9 months ago
Future readers looking to upgrade their code from the now deprecated and removed MySQL extension may be interested in this automated tool -
To Top