Blogger Information
Blog 56
fans 3
comment 1
visits 50787
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO数据库操作函数库——2018年4月27日
沈斌的博客
Original
1194 people have browsed it

PDO数据库操作函数库,增删改查,查询包括单条件查询和多条件查询

function_pdo.php

实例

<?php
/**
 *
 */
if (!function_exists('connect')){
    /**
     * @param $dbname
     * @param string $type
     * @param string $host
     * @param string $charset
     * @param int $port
     * @param string $user
     * @param string $password
     * @return PDO
     */
    function connect($dbname,$type='mysql',$host='127.0.0.1',$charset='utf8',$port=3306,$user='root',$pass='root'){
        $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::ATTR_PERSISTENT => true,

        ];

        try {
            //PDO
            $pdo=new PDO($dsn,$user,$pass,$options);

        } catch (PDOException $e) {
            die('connect error!:'.$e->getMessage());
        }

        return $pdo;
    }
}

if (!function_exists('insert')){
    /**
     * @param $pdo
     * @param $table
     * @param array $data
     * @return bool
     */
    function insert($pdo,$table,$data=[]){
        $sql="INSERT {$table} SET ";
        foreach (array_keys($data) as $field) {
            $sql.=$field.'=:'.$field.', ';
        }

        //去掉逗号
        $sql=rtrim(trim($sql),',').';';

        $stmt=$pdo->prepare($sql);
//        die($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')) {
    /**
     * @param $pdo
     * @param $table
     * @param array $data
     * @param string $where
     * @return bool
     */
    function update($pdo,$table,$data=[],$where='') {
        $sql="UPDATE {$table} SET ";

        foreach (array_keys($data) as $field) {
            $sql.=$field.'=:'.$field.', ';
        }

        //去掉逗号
        $sql=rtrim(trim($sql),',');

        if (!empty($where)) {
            $sql.=' WHERE '.$where;
        } else {
            exit('条件不能为空');
        }

//        die($sql);
        $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('find')) {
    /**
     * @param $db
     * @param $table
     * @param $fields
     * @param string $where
     * @return bool
     */
    function find($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),',').';';
//        die($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('select')) {
    function select($pdo,$table,$fields,$where='',$order='') {
        $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;
        }
        $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} ";
        if (!empty($where)) {
            $sql.=' WHERE '.$where;
        } else {
            exit('条件不为空');
        }

        $sql=rtrim(trim($sql),',').';';
//        die($sql);
        $stmt=$pdo->prepare($sql);
        if ($stmt->execute()) {
            if($stmt->rowCount()>0){
                return true;
            }
        } else {
            return false;
        }

    }
}

运行实例 »

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


函数库测试

demo.php

实例

<?php
/**
 *
 */
require 'lib/function_pdo.php';

//connect
$type='mysql';
$host='localhost';
$dbname='user';
$type='mysql';
$charset='utf8';
$port=3306;
$user='root';
$pass='root';
$pdo=connect($dbname,$type,$host,$charset,$port,$user,$pass);

//insert
$table='staf';
$data=['name'=>'kotlin','sex'=>1,'age'=>50,'salary'=>7000];
//insert($pdo,$table,$data);

//单条件查询
$table='staf';
$fields=['name','age'];
$where='age >30';
echo '<pre>';
print_r(find($pdo,$table,$fields,$where));

//多条件查询
$table='staf';
$fields=['name','age','salary'];
$where='salary>3000';
$order='salary asc';
echo '<hr>';
print_r(select($pdo,$table,$fields,$where,$order));

//更新测试
$table='staf';
$where='staff_id=3';
$data=['salary'=>5000];
//update($pdo,$table,$data,$where);

//删除
$table='staf';
$where='staff_id=9';
delete($pdo,$table,$where);

运行实例 »

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


数据库参考

pdo.png

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