php封装一个链接数据库的函数(增删改查)

数据库 waitig 465℃ 百度已收录 0评论

1。我们封装一个简单的数据库的增删改查函数,方便使用

我们先写一个config文件来使用里面是你数据库的信息

<?php
define('DB_HOST' , 'localhost');
define('DB_USER' , 'root');
define('DB_PWD' , '');
define('DB_NAME' , 'test');
define('DB_CHARSET' , 'utf8');

接下来我们写个看封装好的函数:

<?php
    /*
*@parme string $host
*@parme string $user
*@parme String $pwd
*@prame string $charset
*@prame string $name
*@prame string $table
*@prame array $data
*
*
*/

function connect($host , $user , $pwd , $charset , $name)
{
    $link = mysqli_connect($host , $user , $pwd);

    if (!$link) {
        //exit('数据库链接失败');
        return false;
    }

    mysqli_set_charset($link , $charset);

    if (!mysqli_select_db($link , $name)) {
        return false;
    }

    return $link;
}


function insert($link , $table , $data)
{
    //insert into $table(字段1,字段2 。。。) values(值1,值2,,,,,)
    //数据是从post提交过来的
    //var_dump($data);
    $keys = array_keys($data); //就是数据库的字段

    //var_dump($keys);  //username , password , sex
    $fields = join(',' , $keys);

    //var_dump($fields);
    $value = array_values($data); //获取就是要插入的数据的值

    $values = join(',' , parseValue($value));

    $sql = "insert into $table($fields) values($values)";
    //var_dump($sql);

    $result = mysqli_query($link , $sql);

    if ($result && mysqli_affected_rows($link)) {
        return mysqli_insert_id($link);
    } else {
        return false;
    }

}


//处理值为字符串的情况 用引号引起来
function parseValue($data)
{
    if (is_string($data)) {
        $data = '\''.$data.'\'';
    } else if (is_array($data)) {
        $data = array_map('parseValue' ,$data);
    } else if (is_null($data)) {
        $data = null;
    }
    return $data;
}


function update($link , $table , $data , $where)
{
    //update $table set usename='??' , password=??? , sex =??? where id = $where;

    $set = join(',' , parseSet($data));

    //var_dump($set);

    $sql = "update $table set $set where $where";
    //echo $sql;
    $result = mysqli_query($link , $sql);

    return $result;


}
//处理修改的时候的set问题
function parseSet($data)
{
    foreach ($data as $key => $value) {
        $value = parseValue($value);

        if (is_scalar($value)) {
            $set[] = $key . '=' . $value;
        }
    }

    return $set;
}



function del($link , $table , $where)
{
    //delte from $table where $wehre;

    $sql = "delete from $table where $where ";

    $result = mysqli_query($link , $sql);

    if ($result && mysqli_affected_rows($link)) {
        //return mysqli_insert_id($link);
        return mysqli_affected_rows($link);
    } else {
        return false;
    }
}


function select($link , $table , $where , $fields = '*')
{
    $sql = "select $fields from $table where $where";

    $result = mysqli_query($link , $sql);

    if ($result && mysqli_affected_rows($link)) {

        while ($rows = mysqli_fetch_assoc($result)) {
            $data[] = $rows;
        }
        return $data;
    } else {
        return false;
    }

}

//$data = select($link , 'bbs_user' , 'id>40');

//var_dump($data);

function sum($link , $table , $fields = 'id')
{
    $sql = "select sum($fields) as sum from $table";

    $result = mysqli_query($link , $sql);

    $sum = mysqli_fetch_assoc($result);

    return $sum['sum'];
}


//echo sum($link , 'bbs_user');
function MyMax($link , $table , $fields = 'id')
{
    $sql = "select max($fields) as max from $table";

    $result = mysqli_query($link , $sql);

    $max = mysqli_fetch_assoc($result);

    return $max['max'];
}
//echo MyMax($link , 'bbs_user');

function MyCount($link , $table , $fields = 'id')
{
    $sql = "select count($fields) as count from $table";

    $result = mysqli_query($link , $sql);

    $count = mysqli_fetch_assoc($result);

    return $count['count'];
}

//echo MyCount($link , 'bbs_user');

function MyAvg($link , $table , $fields = 'id')
{
    $sql = "select avg($fields) as avg from $table";

    $result = mysqli_query($link , $sql);

    $avg = mysqli_fetch_assoc($result);

    return $avg['avg'];
}
//求最小值
function MyMin($link,$table,$fields = 'id'){
    $sql = "select min($fields) as min from $table";
    $result = mysqli_query($link,$sql);
    $min = mysqli_fetch_assoc($result);

    return $min['min'];
}

//封装一个分组的方法

function group($link,$table,$fields = "id",$group = "province"){
    $sql = "select count($fields) as a from $table group by $group";
    var_dump($sql);
    $result = mysqli_query($link,$sql);
    if($result && mysqli_affected_rows($link)){
        while($arr = mysqli_fetch_assoc($result)){
                $arrsy[] = $arr['a'];
        }
        return $arrsy;
    }else{
        return false;
    }

}

4.我们这里写个公共类来存放,方便以后开发

    <?php
    session_start();
    //define('WEB_SITE' , str_replace('\\' , '/' , __DIR__));


    include 'config.php';
    include 'mysql_func.php';

    //
    $link = connect(DB_HOST , DB_USER , DB_PWD , DB_CHARSET , DB_NAME);

本文由【waitig】发表在等英博客
本文固定链接:php封装一个链接数据库的函数(增删改查)
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)