Files
knowledge-kit/Chapter3 - Server/3.1.md
2020-02-25 17:46:51 +08:00

156 lines
5.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 利用分页和模糊查询技术实现一个App接口
摘要: 模糊查询技术、分页技术、App接口
1、分页问题
关键技术点php执行sql语句。示例“SELECT * FROM hotel WHERE subject='10' and address like '%杭州%' LIMIT 0,4”。注意LIMIT后需要跟2个数字1个是起始位置2是结束位置。
可以对limit函数进行封装select *from user limit (page-1)*size,page*size;
单例模式编写App接口的注意点
1、php后台获取参数之后根据参数转换成SQL语句
2、php执行SQL语句将结果转换成JSON返回给客户端
2、模糊搜索
SQL匹配模式开发中应用最多的一种
1、使用SQL匹配模式不能使用操作符 = 或 != 而是使用操作符LIKE或者NOT LIKE
2、使用SQL匹配模式MySQL提供了2种通配符。
%:表示任意数量的任意字符包括0个
_:表示任意的单个字符
例子u_name为“张三”“张猫三”、“三脚猫”“唐三藏”。如果要找回包含“三”和“猫”的纪录
“select *from user where u_name LIKE '%三%' and u_name LIKE '%猫%';
若使用“select *from user where u_name LIKE '%三%猫%';”只能找出:三脚猫
3、使用SQL匹配模式如果匹配格式当中不包含以上2种通配符中的任意1个其查询效果等同于= 或 !=
4、使用SQL匹配模式默认情况下是不区分大小写的
正则表达式匹配模式(不推荐):
1、 []:表示括号内所列字符中的一个:指定一个字符、字符串或范围,要求所匹配对象为它们中的任意一个
比如 SELECT * FROM [user] WHERE u_name LIKE '[张李王]三'。将找出“张三”、“李三”、“王三”(而不是“张李王三”);
比如[ ] 内有一系列字符01234、abcde之类的则可略写为“0-4”、“a-e”。SELECT * FROM [user] WHERE u_name LIKE '老[1-9]'-> 将找出“老1”、“老2”、……、“老9”
2、[^]:表示不在括号所列之内的单个字符。其取值和[]相同,要求匹配对象为指定字符以外的任意一个字符
比如 SELECT * FROM [user] WHERE u_name LIKE '[^张李王]三'。将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等
3、.:匹配任意的单个字符
   4、^:表示以某个字符或字符串开头。^a:以a开头
   5、$:表示以某个字符或字符串结尾。s$:以s结尾
   6、*:匹配0个或多个在它前面的字符
使用正则表达式匹配的操作符是REGEXP和NOT REGEXPRLIKE或NOT RLIKE
注意啊正则表达式和SQl匹配模式工作原理不一样正则只要匹配出符合条件就马上算成功而SQl需要严格匹配。比如同样需要写从酒店表中拿出地址中包含“杭州”的酒店
SQL:SELECT *FROM hotel WHERE address LIKE '%杭州%';
正则SELECT *FROM hotel WHERE address REGEXP '^[杭][州]*;
需要特别注意的是:如果所查询的内容包含通配符时,导致我们查询"%","_","["的语句无法正常实现。因此我们需要特殊处理。
需要特别注意的是:如果所查询的内容包含通配符时,导致我们查询"%","_","["的语句无法正常实现。因此我们需要特殊处理。
```php
function sql_encode($sql){
$sql = replace($sql,"[","[[]");
$sql = replace($sql,"%","[%]");
$sql = replace($sql,"_","[_]");
return $sql;
}
```
最后贴一个利用模糊查询实现分页查询的接口。
```php
<?php
/**
* Created by PhpStorm.
* User: geek
* Date: 2017/2/20
* Time: 上午9:15
*/
header('content-type:text.html;charset=utf-8');
error_reporting(0);
require_once '../../model/PdoMySQL.class.php';
require_once '../../model/config.php';
require_once 'Response.php';
class HotelList
{
private $tableName = "hotel";
private $telephone = "";
private $cityName= "";
private $subjectId = "";
private $page = 0;
private $size = 0;
protected static $_instance = null;
protected function __construct()
{
}
protected function __clone()
{
// TODO: Implement __clone() method.
}
public function getInstance()
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
function getHotels()
{
self.$this->telephone = $_REQUEST["telephone"];
self.$this->subjectId = $_REQUEST["subjectId"];
self.$this->cityName = $_REQUEST["cityName"];
self.$this->page = $_REQUEST["page"];
self.$this->size = $_REQUEST["size"];
$mysqlPdo = new PdoMySQL();
if($this->telephone == ""){
Response::show(201,"fail","非安全的数据请求","json");
}
$userRows = $mysqlPdo->find("user","telephone='$this- >telephone'");
if($userRows[0]["telephone"] != $this->telephone){
Response::show(201,"fail","非安全的数据请求","json");
}
$city = str_replace("","",$this->cityName);
$allrows = $mysqlPdo->find($this->tableName,"subject='$this->subjectId' and address like '%$city%'","","","","",[(intval($this->page)-1)*intval($this->size),intval($this->page)*intval($this->size)]);
Response::show(200,'酒店列表获取成功',$allrows,'json');
}
}
$lister = HotelList::getInstance();
$lister->getHotels();
```
其中Respone和PdoMySQL的2个类分别是接口数据展示的类和PDO操作的一个封装。