开发者

ADODB to PDO quick switch

In past i use adodb for mysql in php. Now, i want开发者_开发问答 to use PDO. Is there any way that for me to quick switch from adodb query to pdo using one connection to datebase?


You may want to check the following two examples:

#PDO script:
$db = new PDO("mysql:dbname=you_db_name;host=127.0.0.1", "root", "");
$rs = $db->query("SELECT * FROM table")->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $r) {}

#ADOdb script
require_once("adodb5/adodb.inc.php");
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$db = NewADOConnection("mysql://root:@127.0.0.1/you_db_name");
$rs = $db->Execute("SELECT * FROM table");
foreach($rs as $r) {}

Note that PDO is a native compiled library and not loaded at runtime.


<?php
class Config
{
    private $host = "localhost";
    private $user =  "root";
    private $password =  "";
    private $dbname =  "php_crud";

    public function connect(){

        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        $pdo =  new PDO($dsn,$this->user,$this->password);
        $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
        return $pdo;
    }
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜