rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » PHP

pdo封装(一)

在使用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'=>'jialiang@163.com');
//$tablename='fh_users';

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

//修改数据库
//$params = array('login_ip'=>'172.2.2.1','username'=>'niuhuidong','password'=>'niuniu','email'=>'niu@163.com');
//$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);
?>

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

顶一下
(0)
踩一下
(0)