数据库的多重查询

数据库的多重查询

在使用数据库查询的时候,一般都会显示分页查询的状态,由于每次查询的时候,用户所输入(选择)查询的字段都是不确定的,如果仅仅使用SQL语句进行查询的话是非常麻烦的。

因此改用用户在添加where条件语句的时候就进行相应的拼接,这样会大大的方便sql语句的编写。

例如

//以下三个字段是从用户的表单中来获取数据
        $username = $_GET['username'];
        $name = $_GET['name'];
        $sex = $_GET['sex'];
        if (!empty($username)) { //用户名
            $where.=" and username='" . $username . "'";
            $this->assign('username', $username);
        }
        if (!empty($name)) { //姓名
            $where .= " and name='" . $name . "' ";
            $this->assign('name', $name);
        }
        if (!empty($sex)) { //性别
            $where .= " and sex='" . $sex . "' ";
            $this->assign('sex', $sex);
        }

这样就自动的拼接好了where查询的字段,然后传给模型,在模型里面写SQL语句就是十分的方便了。

原文地址:https://www.cnblogs.com/xs-yqz/p/6010935.html