Blogger Information
Blog 19
fans 0
comment 0
visits 11109
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月27日_php基础13之把php公用方法库中的数据库操作函数, 重写到Db类中--php培训9期线上班
炭烧鸡腿卤煮米线
Original
445 people have browsed it

把 php公用方法库中的 数据库操作函数, 重写到Db类中

<?php
    class Db{
        public $dsn;
        public $user;
        public $password;

        public $pdo;//连接属性,连接好之后给的值

        public function __construct($dsn,$user,$password)
        {
            $this->dsn=$dsn;
            $this->user=$user;
            $this->password=$password;
            $this->connect();
        }
        public function connect(){
            $this->pdo = new PDO($this->dsn,$this->user,$this->password);
        }
        //查询方法
        public function select($table,$fields, $where='', $order='',$limit=''){
            $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;
            }
            //排序条件
            if(!empty($order)) {
                $sql .= ' order by '.$order;
            }
            //分页条件
            if(!empty($limit)) {
                $sql .= ' limit '.$limit;
            }
            $sql .= ';';
            //创建PDO预处理对象
            $stmt = $this->pdo->prepare($sql);
            //执行查询操作
            if($stmt->execute()){
                if($stmt->rowCount()>0){
                    $stmt->setFetchMode(PDO::FETCH_ASSOC);
                    //返回一个二维数组
                    return $stmt->fetchAll();
                }
            } else {
                return false;
            }
        }
        //单条数据
        public function find($table,$fields,$where=''){
            //创建SQL语句
            $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;';
            //创建PDO预处理对象
            $stmt = $this->pdo->prepare($sql);
            //执行查询操作
            if($stmt->execute()){
                if($stmt->rowCount()>0){
                    $stmt->setFetchMode(PDO::FETCH_ASSOC);
                    return $stmt->fetch();
                }
            } else {
                return false;
            }
        }
        //新增数据
        public function insert($table,$data=[]){
            //创建SQL语句
            $sql = "INSERT INTO {$table} SET ";
            //组装插入语句
            if(is_array($data)){
                foreach ($data as $k=>$v) {
                    $sql .= $k.'="'.$v.'", ';
                }
            }else{
                return false;
            }
            //去掉尾部逗号,并添加分号结束
            $sql = rtrim(trim($sql),',').';';
            //创建PDO预处理对象
            $stmt = $this->pdo->prepare($sql);
            //执行新增操作
            if($stmt->execute()){
                if($stmt->rowCount()>0){
                    return 'success'.$stmt->rowCount().'count';
                }
            } else {
                return false;
            }
        }
        //更新数据
        public function update($table,$data=[], $where=''){
            //创建SQL语句
            $sql = "UPDATE {$table} SET ";
            //组装修改语句
            if(is_array($data)){
                foreach ($data as $k=>$v) {
                    $sql .= $k.'="'.$v.'", ';
                }
            }
            //去掉尾部逗号,并添加分号结束
            $sql = rtrim(trim($sql),',');
            //查询条件
            if(!empty($where)){
                $sql .= ' WHERE '.$where;
            }
            //创建PDO预处理对象
            $stmt = $this->pdo->prepare($sql);
            //执行新增操作
            if($stmt->execute()){
                if($stmt->rowCount()>0){
                    return 'update success '.$stmt->rowCount().' count';
                }
            } else {
                return false;
            }
        }
        //删除数据
        public function delete($table,$where=''){
            //创建SQL语句
            $sql = "DELETE FROM {$table}  ";
            //查询条件
            if(!empty($where)){
                $sql .= ' WHERE '.$where;
            }
            //创建PDO预处理对象
            $stmt = $this->pdo->prepare($sql);
            //执行删除操作
            if($stmt->execute()){
                if($stmt->rowCount()>0){
                    return 'delete success '.$stmt->rowCount().' count';
                }
            } else {
                return false;
            }
        }
        //统计数量
        public function count_num($table,$where=''){
            $sql  = 'SELECT count(*) as count_number FROM '.$table;
            //查询条件
            if(!empty($where)){
                $sql .= ' WHERE '.$where;
            }
            //创建PDO预处理对象
            $stmt = $this->pdo->prepare($sql);
            //执行查询操作
            if($stmt->execute()){
                if($stmt->rowCount()>0){
                    $row  = $stmt->fetch(PDO::FETCH_ASSOC);
                    $rows = $row['count_number'];
                    return $rows;
                }
            } else {
                return false;
            }
        }
        public function __destruct()
        {
            $this->pdo = null;
        }
    }

连接数据库

  $db =  new Db('mysql:host=localhost;dbname=shy','root','root');

查询多条数据

echo '<pre>'.print_r($db->select('shoes','pinpai,color','','color DESC'),true);

查询多条数据.png

查询单条数据

echo '<pre>'.print_r($db->find('shoes','number,pinpai','id=5'),true);

查询单条数据.png

新增数据

echo $db->insert('people',['name'=>'she','age'=>18]);

新增数据.png

更新数据

echo $db->update('people',['name'=>'小宇'],'id=1');

更新数据.png

删除数据

echo $db->delete('people','id=4');

删除.png

统计数量

echo $db->count_num('shoes');


统计数量.png

手写:

 

 

开始.png

查询数据.png

单挑.png

新增数据.png

更新数据.png

删除数据.png

统计+关闭连接.png

应用.png

总结:

将数据库函数重写到类中,数据操作方便,提高执行效率

ps:最近家里出了点事一直没时间写作业,一定补齐

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

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