Blogger Information
Blog 48
fans 3
comment 1
visits 37302
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
将数据库常用操作封装——2018年4月26日
JackBlog
Original
757 people have browsed it

实现以下功能:

1、连接数据库

2、带条件更新数据

3、新增数据

4、查询单条数据

5、查询多条数据

6、带条件删除数据


实例

<?php
//连接数据库
if(!function_exists(connect))
{
    /**
     * @param string $dbname
     * @param string $type
     * @param string $host
     * @param string $dbuser
     * @param string $dbpass
     * @param string $charset
     * @param int $port
     * @return PDO
     */
    function connect($dbname='xy28',$type='mysql',$host='127.0.0.1',$dbuser='xy28',$dbpass='123456',$charset='utf8',$port=3306)
    {
        $dsn = "$type:host=$host;dbname=$dbname;charset=$charset;port=$port";
        $options = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  //设置错误模式
            PDO::ATTR_CASE => PDO::CASE_NATURAL,  //数据表字段保持不变
            PDO::ATTR_EMULATE_PREPARES => true, //启用PDO模拟
            PDO::ATTR_PERSISTENT => true, //启用持久性连接
        ];
        try{
            $pdo = new PDO($dsn, $dbuser, $dbpass, $options);
    }catch (PDOException $error){
            die('Connect ERROR!:'.$error->getMessage());
        }
        return $pdo;
    }
}

//数据插入
if (!function_exists(insert)){
    /**
     * @param $pdo
     * @param $table
     * @param array $data
     * @return bool
     */
    function insert($pdo,$table,$data=[])
    {
        $sql = "INSERT IGNORE {$table} SET ";
//        遍历参数$data,拼接到sql语句中
        foreach (array_keys($data) as $fields){
            $sql .= "{$fields} = :{$fields},";
        }
//        删除最右边的','并加上';'
        $sql = rtrim(trim($sql),',').';';
//        创建pdo预处理stmt对象
        $stmt = $pdo->prepare($sql);
//        绑定参数
        foreach ($data as $field=>$value){
            $stmt->bindValue(":{$field}",$value);
        }
        if ($stmt -> execute()){
            if ($stmt->rowCount()>0){
                return true;
            }
        }else{
            return false;
        }
    }
}

//更新数据
if (!function_exists(update)){

    function update($pdo,$table,$data=[],$where='')
    {
        $sql = "UPDATE {$table} SET ";
//        遍历参数$data,拼接到sql语句中

            foreach (array_keys($data) as $fields){
                $sql .= "{$fields} = :{$fields},";
            }

//        删除最右边的','并加上';'
        $sql = rtrim(trim($sql),',');
            if (!empty($where)){
                $sql .= " WHERE {$where};";
            }else{
                exit('条件不得为空');
            }


//        创建pdo预处理stmt对象
        $stmt = $pdo->prepare($sql);
//        绑定参数
        foreach ($data as $field=>$value){
            $stmt->bindValue(":{$field}",$value);
        }
//        执行更新操作
        if ($stmt -> execute()){
            if ($stmt->rowCount()>0){
                return true;
            }
        }else{
            return false;
        }
    }
}
//查询单条数据
if (!function_exists(select)){
    function select($pdo,$table,$fields='*',$where=''){
        $sql='SELECT ';
        if (is_array($fields)){
            foreach ($fields as $field){
                $sql .= $field.',';
            }
        }else{
            $sql.= $fields;
        }
        $sql = rtrim(trim($sql),',');
        $sql .=" FROM {$table}";
        if (!empty($where)){
            $sql.= " WHERE {$where}";
        }
        $sql .= ' LIMIT 1';
        $sql = rtrim(trim($sql),',').';';

        $stmt=$pdo->prepare($sql);
//        执行
        if ($stmt->execute()){
            if ($stmt->rowCount()>0){
                $stmt->setFetchMode(PDO::FETCH_ASSOC);
                return $stmt->fetch();
            }
        }else{
            return false;
        }


    }
}
//查询多条数据
if (!function_exists(selectmore)){
    function selectmore($pdo,$table,$fields='*',$where='',$order,$sort){
        $sql='SELECT ';
        if (is_array($fields)){
            foreach ($fields as $field){
                $sql .= $field.',';
            }
        }else{
            $sql.= $fields;
        }
        $sql = rtrim(trim($sql),',');
        $sql .=" FROM {$table}";
        if (!empty($where)){
            $sql.= " WHERE {$where}";
        }
        $sql .= " ORDER BY {$order} {$sort}";
        $sql = rtrim(trim($sql),',').';';

        $stmt=$pdo->prepare($sql);
//        执行
        if ($stmt->execute()){
            if ($stmt->rowCount()>0){
                $stmt->setFetchMode(PDO::FETCH_ASSOC);
                return $stmt->fetchAll();
            }
        }else{
            return false;
        }


    }
}
//删除数据
if (!function_exists(delete)){

    function delete($pdo,$table,$where='')
    {
        $sql = "DELETE FROM {$table}";
//        遍历参数$data,拼接到sql语句中

//
        if (!empty($where)){
            $sql .= " WHERE {$where}";

        }else{
            exit('条件不得为空');
        }
        $sql = rtrim(trim($sql),',').';';
//        创建pdo预处理stmt对象
        $stmt = $pdo->prepare($sql);


//        执行删除操作
        if ($stmt -> execute()){
            if ($stmt->rowCount()>0){
                return true;
            }
        }else{
            return false;
        }
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!