程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> pdo封裝(一)

pdo封裝(一)

編輯:PHP綜合

在使用pdo這個封裝類之前,注意要對apache的配置文件PHP.ini進行修改.

open-->php.ini-->Ctrl+F-->PHP_pdo_MySQL_dll(開啟它[去掉前面的;])

如果童鞋忘記了具體開啟哪一個制定的配置信息.那麼就查詢pdo凡是帶有pdo的文件就開啟.

PHP5的pdo未來主流.很好很強大.

步驟一:下面的內容另存為 DbPdo.PHP

<?PHP
class DbPdo     private $connection = null;
    private $stmt = null;
    public function __construct($config)         try{
             $this->connection = new PDO('MySQL:host='.$config['DB_HOST'].';dbname='.$config['DB_NAME'], $config['DB_USER'], $config['DB_PWD']);
             $this->connection->exec('SET NAMES utf8');         catch (PDOException $e) {
            echo $e->getMessage(), 'host:', $config['DB_HOST'], ';dbname:', $config['DB_NAME'],
            $config['DB_USER'], $config['DB_PWD'];     }
    public function insert($tablename, $params)      $fIElds = array_keys($params);
     $field = implode(',', $fIElds);
     $question = str_repeat('?, ', count($params));
     $question = rtrim($question, ', ');
        $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
            $tablename,
            $fIEld,
            $question);
        $result = $this->query($sql, $params);
        return $this->lastInsertId();     public function update($tablename, $params, $where = 1)         $fIElds = array_keys($params);
        $field = implode(' = ?, ', $fIElds).' = ?';
        $sql = sprintf('UPDATE %s SET %s WHERE %s',
            $tablename,
            $fIEld,
            $where);
        $result = $this->query($sql, $params);
        return $this->rowCount();     public function delete($tablename, $where)      $sql = '';
        $result = $this->query($sql, $params);
        return $this->rowCount();     public function fetchOne($sql, $params, $fetchMode = PDO::FETCH_ASSOC)         $this->query($sql, $params);
        return $this->stmt->fetch($fetchMode);     public function fetchAll($sql, $params, $fetchMode = PDO::FETCH_ASSOC)         $queryId = $this->query($sql, $params);
        return $this->stmt->fetchAll($fetchMode);     public function lastInsertId()         try{
            return $this->connection->lastInsertId();         catch (PDOException $e) {
            echo $e->getMessage();     }
    public function rowCount()         try{
            return $this->stmt->rowCount();         catch (PDOException $e) {
            echo $e->getMessage();     }
    public function query($sql, $params)         $this->_prepare($sql);
        $key = 1;
        foreach($params as $param)             $this->_bindParam($key++, $param);         return $this->_execute();     private function _execute()         try{
           $result = $this->stmt->execute();         catch (PDOException $e) {
            echo $e->getMessage();         return $result;     private function _prepare($sql)         try{
            $this->stmt = $this->connection->prepare($sql);         catch (PDOException $e) {
            echo $e->getMessage();     }
    private function _bindParam($key, $param)         try{
             $this->stmt->bindParam($key, $param);         catch (PDOException $e) {
            echo $e->getMessage();     }
}

?>

★★★★★★★★★★★★★★★★★★★★★★★★

步驟二 以下內容另存為 config.PHP

<?PHP
return array(
'DB_TYPE'=> 'MySQL',          // 鏁版嵁搴撶被鍨?

'DB_HOST'=> 'localhost', // 鏁版嵁搴撴湇鍔″櫒鍦板潃

'DB_NAME'=>'fahao',  // 鏁版嵁搴撳悕绉?

'DB_USER'=>'root', // 鏁版嵁搴撶敤鎴峰悕

'DB_PWD'=>'', // 鏁版嵁搴撳瘑鐮?

'DB_PORT'=>'3306', // 鏁版嵁搴撶鍙?

'DB_PREFIX'=>'fh_', // 鏁版嵁琛ㄥ墠缂?
)

?>

★★★★★★★★★★★★★★★★★★★★★★★★

步驟三:以下內容另存為index.PHP

 <?PHP
require_once 'librarIEs/DbPdo.PHP';

$config = require_once 'data/config.PHP';

$pdo = new DbPdo($config);

//$sql = 'INSERT INTO fh_users (username, passWord, email, login_ip, ts_reg, ts_login)';
//$sql .= ' VALUES (?, ?, ?, ?, ?, ?) ';
//$params = array('us_ss3', '123456', , '127.0.0.1', '2010-05-24', '2010-05-24'); //$params = array('login_ip'=>'172.0.0.1','username'=>'gengjialiang','passWord'=>'123456','email'=>'[email protected]');
//$tablename='fh_users';

//$result = $pdo->insert($tablename,$params);

//修改數據庫
//$params = array('login_ip'=>'172.2.2.1','username'=>'niuhuidong','passWord'=>'niuniu','email'=>'[email protected]');
//$tablename='fh_users';
//$result = $pdo->update($tablename,$params);

//查詢數據庫

//查詢多條數據
//$sql = 'SELECT * FROM fh_users WHERE user_id in (?, ?)';
$sql = 'SELECT * FROM fh_users WHERE user_id in (?)';
$params = array(4);
$result = $pdo->fetchAll($sql, $params, PDO::FETCH_NUM);
echo '<pre>';
//$result = $pdo->delete(fh_users,58);
print_r($result);
?>

★★★★★★★★★★★★★★★★★★★★★★★★

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved