批改状态:合格
老师批语:
tp6数据库语句的拼凑一般根据前台页面返回的数据从数据库取的数据根据不同的选择查询不同的数据库表
数据库查询规则:sql语句格式, select查询 字段 from来自表 where条件 LIKE '%g'条件中的条件 order by cou_id来自某个字段排序规则(正序倒叙) LIMIT查询的数量/页数
其中select为查询 值一般为某个表的字段,可以多个字段from为来自的表,值一般是数据库的某一个表where一般为数据库表某个字段的 比如status=1,也就是表的字段为status值为1,LIKE一般和where条件一起出现。
使用%作为通配符, 1%查询status条件为1开头,%1是查询status条件为1末尾的,%1%是查询status包含1的
解释:https://www.w3school.com.cn/sql/sql_like.asporder by排序的规则,值为asc正序 desc倒序LIMIT查询的数量 值一般为2个,第一个值为偏移量,第二个值为页数
w3c写的有点乱看这里也可以:
http://www.ouyangke.net/back/php/17MySQl%E8%AF%AD%E5%8F%A5.html
数据库查询一般都是分为4步,为了更好循环利用查询语句
就是将查询语句 也就是select作为一个变量
条件作为一个变量
排序作为一个变量
以及分页作为一个变量
最后统一调用
发一个示例代码:
代码示例:
<?phpdeclare(strict_types=1);namespace app\admin\controller;use think\Request;use think\facade\Db;class Course{/*** 显示资源列表** @return \think\Response*/public function index(){//判断当前是post还是get请求// ispost()助手函数 判断是什么请求if (request()->ispost()) {$data = request()->param();$draw = $data['draw'];// 下面是排序的列数偏移量$order_column = $data["order"]["0"]["column"];// 获取排序的规则$order_dir = $data["order"]["0"]["dir"];$start = $data["start"];// 拿到每页输出的值$length = $data["length"];$orderSql = "";if (isset($order_column)) {$i = intval($order_column);switch ($i) {case '0':$orderSql .= " order by cou_id " . $order_dir . " ";break;default:$orderSql = " ";break;}}/**分页参数**/$start = $data['start'];/**从多少开始**/$length = $data['length'];// 拼接分页的mysql$limitSql = '';$limitSql .= " LIMIT " . intval($start) . "," . intval($length);// 查询总条数语句$totalsql = "SELECT count(cou_id) as sun FROM mj_course_lists WHERE status=1";// 声明一个过滤以后的条数$recordsFiltered = 0;// 数据表里面的总条数$recordsTotal = 0;// 获取一下总的数组$resTotal = Db::query($totalsql);// dd($resTotal); 获取总条数精确到数字 也就是拿到数组的值$recordsTotal = $resTotal[0]['sun'];//拼接搜索mysql$search = $data["search"]["value"];$searchsql = " and (cou_id LIKE '%" . $search . "%' or title LIKE '%" . $search . "%')";// 有过滤条件if (strlen($search) > 0) {$recordsFilteredResult = Db::query($totalsql . $searchsql);$recordsFiltered = $recordsFilteredResult[0]['sun'];} else {//没有过滤条件 意味着过滤后的条数=总条数$recordsFiltered = $recordsTotal;}$mainsql = 'select cou_id,title,info,pic from mj_course_lists where status=1';// dd($mainsql . $searchsql . $orderSql . $limitSql);// 取数据表所有的(过滤)数据// sql语句格式, select查询 字段 from来自表 where条件 LIKE '%g'条件中的条件 order by cou_id来自某个字段排序规则(正序倒叙) LIMIT查询的数量/页数$tableData = Db::query($mainsql . $searchsql . $orderSql . $limitSql);echo json_encode(["draw" => intval($data['draw']),'recordsTotal' => $recordsTotal,'recordsFiltered' => $recordsFiltered,'tableData' => $tableData], 320);} else {return view();}}/*** 显示创建资源表单页.** @return \think\Response*//*** 保存新建的资源** @param \think\Request $request* @return \think\Response*/public function save(Request $request){//}/*** 显示指定的资源** @param int $id* @return \think\Response*/public function read($id){//}/*** 显示编辑资源表单页.** @param int $id* @return \think\Response*/public function edit($id){//}/*** 保存更新的资源** @param \think\Request $request* @param int $id* @return \think\Response*/public function update(Request $request, $id){//}/*** 删除指定资源** @param int $id* @return \think\Response*/public function delete($id){//}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号