MYSQL select 封装

 /**
     * @author zWX212114
     * @todo MYSQL select
     * @param $sql SQL语句字符串
     * @param $params 条件参数数组
     * @return Array $result
     * @example 此处只是示范,具体的参数设置请自行设置
     * $params = array(
     *              'where' => array(
     *                           'and' => array(
     *                                       'id = 1',
     *                                       'title LINKE %标题%', 
     *                                      ),
     *                           'or'  => array(
     *                                       'id = 1',
     *                                       'title LINKE %标题%', 
     *                                      ),
     *                          ),
                    'order' => 'id DESC',
                    'limit' => ($nowpagenum-1)*$pagesize.','.$pagesize,
                    );
        $list = $this->getArticalModel() ->fetchAll($params);
     * @license 此处仅仅是日常使用如有特殊需要请自行处理
     */
    public function dbSelect($sql=null,$params=null){
        $this->dbLink();
        $num = 0;
        //检测是否有SQL语句
        if(empty($sql)){
            $sql = 'SELECT * FROM '.$this->table;
        }else{
            $sql = $sql;
        }
        //检测是否设置有其他附加选项where-and、or,order,limit
        if(!empty($params)){
            if(isset($params['where']) && !empty($params['where'])){
                if(is_array($params['where'])){
                    if(isset($params['where']['and']) && !empty($params['where']['and'])){
                        $whereand = join(' AND ', $params['where']['and']);
                    }else{
                        $whereand = null;
                    }
                    if(isset($params['where']['or']) && !empty($params['where']['or'])){
                        $whereor = join(' OR ', $params['where']['or']);
                    }else{
                        $whereor = null;
                    }
                    if($whereand == null && $whereor == null){
                        $where = ' ';
                    }else{
                        if($whereand == null){
                            $where = ' WHERE '. $whereor;
                        }else if($whereor == null){
                            $where = ' WHERE '.$whereand;
                        }else{
                            $where = ' WHERE '.$whereand .' OR '. $whereor;
                        }
                    }
                }else{
                    $where = ' WHERE '. $params['where'];
                }
            }else{
                $where = ' ';
            }
            if(isset($params['order']) && !empty($params['order'])){
                $order = ' ORDER BY '.$params['order'];
            }else{
                $order = ' ';
            }
            if(isset($params['limit']) && !empty($params['limit'])){
                $limit = ' LIMIT '.$params['limit'] . ' ; ';
            }else{
                $limit = ' ';
            }
            $sqlString = $sql.$where.$order.$limit;
        }else{
            $sqlString = $sql;
        }
        //调试输出SQL语句
        //echo $sqlString;
        $query = mysql_query($sqlString);
        $result = array();
        while($row = mysql_fetch_array($query)){
            $result[$num] = $row; 
            $num++;
        }
        mysql_free_result($query); 
        return $result;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值