PDO封装函数

header("Content-type: text/html; charset=utf-8"); 
    
    /**
     * 初始化 pdo 对象实例
     * @param bool $newinstance 是否要创建新实例
     * @return object->PDO
     */
    function pdo($newinstance = false){
        global $_W;
        if($newinstance) {
            $host = 'localhost';
            $dbname = 'pro';
            $dsn = "mysql:host={$host};dbname={$dbname}";
            $username = 'root';  
            $passwd = 'root';
            $charset = 'UTF8';
            //连接数据库
            $db = new PDO($dsn, $username, $passwd);
            $sql = "SET NAMES $charset;";
            $db->exec($sql);
            return $db;
        } else {
            if(empty($_W['pdo'])) {
                $_W['pdo'] = $GLOBALS['pdo'] = pdo(true);
            }
            return $_W['pdo'];
        }
    }
    
    /**
     * 执行一条非查询语句
     *
     * @param string $sql
     * @param array or string $params
     * @return mixed
     *          成功返回受影响的行数
     *          失败返回FALSE
     */
    function pdo_query($sql, $params = array()) {
        if (empty($params)) {
            $result = pdo()->exec($sql);
            return $result;
        }
        //echo $sql;
        $statement = pdo()->prepare($sql);
        $result = $statement->execute($params);
        if (!$result) {
            return false;
        } else {
            return $statement->rowCount();
        }
    }
    
    /**
     * 执行SQL返回第一个字段
     *
     * @param string $sql
     * @param array $params
     * @param int $column 返回查询结果的某列,默认为第一列
     * @return mixed
     */
    function pdo_fetchcolumn($sql, $params = array(), $column = 0) {
        $statement = pdo()->prepare($sql);
        $result = $statement->execute($params);
        if (!$result) {
            return false;
        } else {
            return $statement->fetchColumn($column);
        }
    }

    /**
     * 执行SQL返回第一行
     *
     * @param string $sql
     * @param array $params
     * @return mixed
     */
    function pdo_fetch($sql, $params = array()) {
        $statement = pdo()->prepare($sql);
        $result = $statement->execute($params);
        
        if (!$result) {
            return false;
        } else {
            return $statement->fetch(pdo::FETCH_ASSOC);
        }
    }
    
    /**
     * 执行SQL返回全部记录
     *
     * @param string $sql
     * @param array $params
     * @return mixed
     */
    function pdo_fetchall($sql, $params = array(), $keyfield = '') {
        $statement = pdo()->prepare($sql);
        $result = $statement->execute($params);
        
        if (!$result) {
            return false;
        } else {
            if (empty($keyfield)) {
                return $statement->fetchAll(pdo::FETCH_ASSOC);
            } else {
                $temp = $statement->fetchAll(pdo::FETCH_ASSOC);
                $rs = array();
                if (!empty($temp)) {
                    foreach ($temp as $key => &$row) {
                        if (isset($row[$keyfield])) {
                            $rs[$row[$keyfield]] = $row;
                        } else {
                            $rs[] = $row;
                        }
                    }
                }
                return $rs;
            }
        }
    }

    /**
     * 更新记录
     *
     * @param string $table
     * @param array $data
     *          要更新的数据数组
     *          array(
     *              '字段名' => '值'
     *          )
     * @param array $params
     *          更新条件
     *          array(
     *              '字段名' => '值'
     *          )
     * @param string $gule
     *          可以为AND OR
     * @return mixed
     */
    function pdo_update($table, $data = array(), $params = array(), $gule = 'AND') {
        global $_W;
        $fields = pdo_implode($data, ',');
        $condition = pdo_implode($params, $gule);
        $params = array_merge($fields['params'], $condition['params']);
        $sql = "UPDATE $table SET {$fields['fields']}";
        $sql .= $condition['fields'] ? ' WHERE '.$condition['fields'] : '';
        return pdo_query($sql, $params);
    }
    
    /**
     * 删除记录
     *
     * @param string $table
     * @param array $params
     *          更新条件
     *          array(
     *              '字段名' => '值'
     *          )
     * @param string $gule
     *          可以为AND OR
     * @return mixed
     */
    function pdo_delete($table, $params = array(), $gule = 'AND') {
        global $_W;
        $condition = pdo_implode($params, $gule);
        $sql = "DELETE FROM $table ";
        $sql .= $condition['fields'] ? ' WHERE '.$condition['fields'] : '';
        return pdo_query($sql, $condition['params']);
    }
    
    /**
     * 插入数据
     *
     * @param string $table
     * @param array $data
     *          要更新的数据数组
     *          array(
     *              '字段名' => '值'
     *          )
     * @param boolean $replace
     *          是否执行REPLACE INTO
     *          默认为FALSE
     * @return mixed
     */
    function pdo_insert($table, $data = array(), $replace = FALSE) {
        global $_W;
        $cmd = $replace ? 'REPLACE INTO' : 'INSERT INTO';
        $condition = pdo_implode($data, ',');
        //echo "$cmd $table SET {$condition['fields']}";
        //var_dump($condition['params']);
        return pdo_query("$cmd $table SET  {$condition['fields']}", $condition['params']);
    }
    
    /**
     * 返回lastInsertId
     *
     */
    function pdo_insertid() {
        return pdo()->lastInsertId();
    }
    
    /**
     * 转换PDO的字段与参数列表
     *
     * @param array or string $params
     *          可以是数组或字符串
     *          是字符串直接返回
     * @param string $glue
     *          字段间的分隔符
     *          可以为逗号(,)或是 AND OR 应对不同的SQL
     * @return mixed
     *          array(
     *              'fields' 字段列表或条件
     *              'params' 参数列表
     *          )
     */
    function pdo_implode($params, $glue = ',') {
        $result = array('fields' => ' 1 ', 'params' => array());
        $split = '';
        if (!is_array($params)) {
            $result['fields'] = $params;
            return $result;
        }
        if (is_array($params)) {
            $result['fields'] = '';
            foreach ($params as $fields => $value) {
                $result['fields'] .= $split . "`$fields` =  :$fields";
                $split = ' ' . $glue . ' ';
                $result['params'][":$fields"] = is_null($value) ? '' : $value;
            }
        }
        return $result;
    }
    
    //标明回滚起始点
    function pdo_begin(){
        return pdo()->beginTransaction();
    }
    //标明回滚结束点,并执行SQL
    function pdo_commit(){
        return pdo()->commit();
    }
    //执行回滚
    function pdo_back(){
        return pdo()->rollBack();
    }

 

posted @ 2014-07-10 16:02  壁虎漫步.  阅读(493)  评论(0编辑  收藏  举报