106. 数据库增删改的封装

来源:互联网 发布:刻字机怎么设置端口 编辑:程序博客网 时间:2024/05/05 13:45
<?php// insert into 表名() values(数据1,数据2,...);/*function insert($table,$data){    $cols = array();    $vals = array();    foreach($data as $k=>$v){        $cols[] = $k;        $vals[] = $v;    }    var_dump($cols);    var_dump($vals);die;    //表名()    $tableValue = implode(',',$cols);    //数据    $dataValue = implode(',',$vals);    $sql = "insert into $table($tableValue) values($dataValue);";    return $sql;}$table = 'test';$data = array(    'id'=>1,    'content'=>'hello');$sql = insert($table,$data);var_dump($sql);*/// update 表名 set 键=值 where ../*function update($table,$set,$where=null){    $cols = array();    $vals = array();    $data = array();    foreach($set as $k=>$v){        $data[] = "$k=$v";    }    $data = implode(',',$data);    $sql = "update $table set $data where $where;";    var_dump($sql);die;}$set = array(    'id'=>1,    'content'=>'werwr');$sql = "update test set id='11' content='hwewe100'";$table = 'test';$where = 'id = 1';$sql = update($table,$set,$where);*///delete from 表名 where 条件/*function delete($table,$where){    $sql = "delete from $table where $where;";    var_dump($sql);die;    return $sql;}$table = 'test';$where = 'id = 1';delete($table,$where);*/
0 0
原创粉丝点击