Blogger Information
Blog 29
fans 0
comment 1
visits 18718
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库函数封装实例(补作业)-2018年5月3日
小小的菜的博客
Original
756 people have browsed it

实例

<?php
/**
 *
 */
if (!function_exists('connect'))
{
    /**
     * @param $dbname
     * @param $type
     * @param $host
     * @param $charset
     * @param $port
     */
    function connect($dbname,$type='mysql',$host='localhost',$charset='utf8',$port=3306,$user='root',$pass='root')
    {
        $dsn = "{$type}:host={$host};dbname={$dbname};charset={$charset};port={$port}";
        $userName = $user;
        $password = $pass;
        $options = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,//错误模式
            PDO::ATTR_CASE => PDO::CASE_NATURAL,//自然名称
            PDO::ATTR_EMULATE_PREPARES => true,//启用模拟功能
            PDO::ATTR_PERSISTENT => true,
        ];
        try {
            $pdo = new PDO($dsn, $userName, $password, $options);
//            echo 'ok';
        }catch (PDOException $e){
            print '连接失败'.$e->getMessage();
            die();
        }
        return $pdo;
    }
}

if (!function_exists('insert'))
{
    function insert($pdo, $table, $data=[])
    {
//        insert ignore staff set name=:name, salary=:salary
        $sql = "INSERT IGNORE {$table} SET ";
        foreach (array_keys($data) as $field){
            $sql .= $field.' =:'.$field.',';
        }
        $sql = rtrim(trim($sql), ',').';';
//        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('update'))
{
    function update($pdo, $table, $data=[],$where='')
    {
//        update staff set name=:name, salary=:salary where staff_id=: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('条件不能为空');
        }
        $sql = rtrim(trim($sql), ',').';';

//        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'))
{
    function find($pdo, $table, $fields,$where='')
    {
//        select name from staff where staff_id=:where;
        $sql = "SELECT ";
        if(is_array($fields)){
            foreach($fields as $field){
                $sql .= $fields. ', ';
            }
        }else{
            $sql .= $fields. ',';
        }
        $sql = rtrim(trim($sql), ',');

        $sql .= ' FROM '.$table;
        if(!empty($where)){
            $sql .= ' WHERE '.$where.';';
        }
//        $sql .= ' LIMIT 4;';

//        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='')
    {
//        select name from staff where staff_id=:where;
        $sql = "SELECT ";
        if(is_array($fields)){
            foreach($fields as $field){
                $sql .= $fields. ', ';
            }
        }else{
            $sql .= $fields. ',';
        }
        $sql = rtrim(trim($sql), ',');

        $sql .= ' FROM '.$table;
        if(!empty($where)){
            $sql .= ' WHERE '.$where;
        }
        if(!empty($order)){
            $sql .= ' ORDER BY '.$order.';';
        }else{
            $sql .= ';';
        }

//        die($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='')
    {
//        update staff set name=:name, salary=:salary where staff_id=: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;
        }

    }
}

运行实例 »

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


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