微擎常用数据库操作 - 读写改删

/**
 * 增
 * int|boolen pdo_insert($tablename,$data=array(),$replace=false)
 * $tablename 表名称 要全名 例如 'ims_jscm_hongbao_users'
 * $replace true 是插入数据模式
 */
public function doWebAdd(){
    $query = array('weixinhao'=>'wxbm','nickname'=>'微信保姆','openid'=>'efeefafasfasdfsaf');
    $result = pdo_insert('ims_jscm_hongbao_users',$query,true);
    if($result == 1){
        $resMeg['status'] = 1;
        $resMeg['meg'] = "插入数据成功";
        $resMeg['query'] = $query;
        echo json_encode($resMeg);
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "插入数据失败";
        $resMeg['query'] = $query;
        echo json_encode($resMeg);
    }
}

/**
 * pdo_insert 来实现 改 第2种方法 (不推荐使用,了解即可,修改还是推荐使用pdo_update)
 * int|boolen pdo_insert($tablename,$data=array(),$replace=true)
 * $tablename 表名称 要全名 例如 'ims_jscm_hongbao_users'
 * $replace false 是编辑模式,
 * 注意:$replace = false 这种编辑的方法要把记录的所有字段全部都要列到数组里,
 * 否则其他没有列入的字段会被设为空,或者默认值
 */
public function doWebEdit(){
    global $_GPC;
    $id = $_GPC['id'];
    $query = array('id'=>$id,'weixinhao'=>'wxbm11','nickname'=>'微信保姆11','openid'=>'11eefafasfasdfsaf');
    $result = pdo_insert('ims_jscm_hongbao_users',$query,true);
    if($result == 2){
        $resMeg['status'] = 1;
        $resMeg['meg'] = "编辑数据成功";
        $resMeg['query'] = $query;
        echo json_encode($resMeg);
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "编辑数据失败";
        $resMeg['query'] = $query;
        echo json_encode($resMeg);
    }
}

/**
 * 删
 * int|boolen pdo_delete($tablename,$condition=array(),$glue='AND')
 * $condition 删除条件是个数组
 * $glue 逻辑关系 默认是 AND 可以设置为 OR
 * 解释:当条件数组 $condition 里大于等于2个条件时,可以起作用
 * AND 条件满足 条件1 AND 条件2 AND 条件n 的执行删除
 * 例如
 * $result = pdo_delete('ims_jscm_hongbao_users',$condition=array('id'=>10,'weixinhao'=>'wxbm10'),$glue='AND')
 * 满足 id 和 weixinhao 全部的条件才可删除
 * OR 条件满足 条件1 OR 条件2 OR 条件n 其中任意一条的执行删除
 * 例如
 * $result = pdo_delete('ims_jscm_hongbao_users',$condition=array('id'=>11,'weixinhao'=>'wxbm'),$glue='OR')
 * 满足 id 和 weixinhao 任意一个条件的都执行删除
 *
 * 返回结果 是删除的记录数,0 表示删除失败,或没有删除,1 表示删除了 1条记录 ,3表示删除了3条记录
 */
public function doWebDel(){
    global $_GPC;
    $id = $_GPC['id'];
    $weixinhao = $_GPC['weixinhao'];
    $condition = array('id'=>$id,'weixinhao'=>$weixinhao);
    $result = pdo_delete('ims_jscm_hongbao_users',$condition,'AND');
    if($result > 0){
        $resMeg['status'] = 1;
        $resMeg['meg'] = $result."条记录删除成功";
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "删除数据失败";
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }
}
public function doWebDelOr(){
    global $_GPC;
    $id = $_GPC['id'];
    $weixinhao = $_GPC['weixinhao'];
    $condition = array('id'=>$id,'weixinhao'=>$weixinhao);
    $result = pdo_delete('ims_jscm_hongbao_users',$condition,'OR');
    if($result > 0){
        $resMeg['status'] = 1;
        $resMeg['meg'] = $result."条记录删除成功";
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "删除数据失败";
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }
}

/**
 * 改
 * array | boolean pdo_update($tablename,$data = array(), $condition = array(), $glus='AND')
 *
 * $glue 逻辑关系 默认是 AND 可以设置为 OR
 * 解释:当条件数组 $condition 里大于等于2个条件时,可以起作用
 * AND 条件满足 条件1 AND 条件2 AND 条件n 的执行更新
 * 例如
 * $result = pdo_update('ims_jscm_hongbao_users',$data=array('weixinhao'=>'xiaoguan100'),$condition=array('id'=>1,'weixinhao'=>'xiao-guan'),$glue='AND')
 * 满足 id 和 weixinhao 全部的条件才可更新
 * OR 条件满足 条件1 OR 条件2 OR 条件n 其中任意一条的执行更新
 * 例如
 * $result = pdo_delete('ims_jscm_hongbao_users',$condition=array('id'=>11,'weixinhao'=>'wxbm'),$glue='OR')
 * 满足 id 和 weixinhao 任意一个条件的都执行更新
 */

public function doWebUpdate(){
    global $_GPC;
    $id = $_GPC['id'];
    $weixinhao = $_GPC['weixinhao'];
    $result = pdo_update('ims_jscm_hongbao_users',$query=array('weixinhao'=>'xiaoguan100'),$condition=array('id'=>$id,'weixinhao'=>$weixinhao),$glue='AND');
    if($result > 0){
        $resMeg['status'] = 1;
        $resMeg['meg'] = $result."条记录更新成功";
        $resMeg['query'] = $query;
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "更新数据失败";
        $resMeg['query'] = $query;
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }
}

public function doWebUpdateOr(){
    global $_GPC;
    $id = $_GPC['id'];
    $weixinhao = $_GPC['weixinhao'];
    $result = pdo_update('ims_jscm_hongbao_users',$query=array('weixinhao'=>'jonyguan'),$condition=array('id'=>$id,'weixinhao'=>$weixinhao),$glue='OR');
    if($result > 0){
        $resMeg['status'] = 1;
        $resMeg['meg'] = $result."条记录更新成功";
        $resMeg['query'] = $query;
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "更新数据失败";
        $resMeg['query'] = $query;
        $resMeg['condition'] = $condition;
        echo json_encode($resMeg);
    }

}

/**
 * 查-单条
 * array|boolean pdo_get($tablename, $condition = array(), $fields = array());
 * $condition 条件数组 可多条件
 * $fields 默认不设置为返回所有字段,指定返回的字段
 */

public function doWebGet(){
    global $_GPC;
    $id = $_GPC['id'];
    $result = pdo_get('ims_jscm_hongbao_users',$condition = array('id'=>$id), $fields = array('weixinhao','nickname'));
    $resMeg = [];
    if($result){
        $resMeg['status'] = 1;
        $resMeg['meg'] = "查询数据成功";
        $resMeg['condition'] = $condition;
        $resMeg['data'] = $result;
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "查询数据失败";
        $resMeg['condition'] = $condition;
    }
    echo json_encode($resMeg);
}

/**
 * 查-多条
 * array|boolean pdo_getall($tablename, $condition = array(), $fields = array(), $keyfield = '' ,$orderby = array(), $limit = array());
 * $condition 条件数组 可选项
 * $fields 默认不设置为返回所有字段,指定返回的字段
 */
public function doWebGetAll(){
    global $_GPC;
    $id = $_GPC['id'];
    $weixinhao = $_GPC['weixinhao'];
    $condition = array();
    $fields = array();
    $keyfield = '';//用于指定一个下标名,这个不常用,推荐空着
    $orderby = array();
    $limit = array();
    $condition['weixinhao'] = 'wxbm';
    $condition['id <'] = '26';
    $fields[] = 'id';
    $fields[] = 'weixinhao';
    $fields[] = 'nickname';
    $orderby[] = 'id ASC';
    $limit[] = 1;
    $limit[] = 3;
    $result = pdo_getall('ims_jscm_hongbao_users',$condition,$fields,$keyfield,$orderby,$limit);
    $resMeg = [];
    if($result){
        $resMeg['status'] = 1;
        $resMeg['meg'] = "查询数据成功";
        $resMeg['total'] = count($result);
        $resMeg['condition'] = $condition;
        $resMeg['data'] = $result;
    }else{
        $resMeg['status'] = 0;
        $resMeg['meg'] = "查询数据失败";
        $resMeg['condition'] = $condition;
    }
    echo json_encode($resMeg);
}

/**
 * 查 - 常用带分页列表方法 - pdo_getall 实现
 * 参数 page 页数
 * 参数 pagesize 是每页多少条
 * https://api.qdb2c.com/web/index.php?c=site&a=entry&m=jscm_hongbao&do=pagelist&page=1&pagesize=5
 */
public function doWebPageList(){
    global $_W ,$_GPC;
    //设置分页规则
    $pageindex=max(intval($_GPC['page']),1);
    $pagesize=max(intval($_GPC['pagesize']),5);
    $p=($pageindex-1)*$pagesize;
    $total = count(pdo_getall('ims_jscm_hongbao_users'));
    $pager=pagination($total,$pageindex,$pagesize);
    //读取数据
    $condition = array();
    $fields = array();
    $keyfield = '';//用于指定一个下标名,这个不常用,推荐空着
    $orderby = array();
    $limit = array();
    $orderby[] = 'id asc';
    $limit[] = $p;
    $limit[] = $pagesize;
    $userlist = pdo_getall('ims_jscm_hongbao_users',$condition,$fields,$keyfield,$orderby,array(1,4));
    var_dump($limit);
    echo "<hr/><hr/>";
    var_dump($userlist);
    echo "<hr/>";
    echo($pager);
}

/**
 * 查 - 常用带分页列表方法 - pdo_fetchall 实现
 * 参数 page 页数
 * 参数 pagesize 是每页多少条
 * https://api.qdb2c.com/web/index.php?c=site&a=entry&m=jscm_hongbao&do=pagelist&page=1&pagesize=5
 */
public function doWebPageList2(){
    global $_W ,$_GPC;
    /** 注意tablename写的时候不要带前缀ims_否则会报错,
     * 例如表名为 ims_jscm_hongbao_users
     * 用tablename函数的写法是 tabalename('jscm_hongbao_users')
     */
    $sql="select * from ".tablename('jscm_hongbao_users');
    $sources=pdo_fetchall($sql);
    //分页开始
    $total=count($sources);
    $pageindex=max($_GPC['page'],1);
    $pagesize=$_GPC['pagesize'];
    $pager=pagination($total,$pageindex,$pagesize);
    $p=($pageindex-1)*$pagesize;
    $sql.=" order by id desc limit ".$p.",".$pagesize;
    $userslist = pdo_fetchall($sql);
    echo(json_encode($userslist));
    echo($pager);
}