PHP函数中的奇怪过滤结果
Posted
技术标签:
【中文标题】PHP函数中的奇怪过滤结果【英文标题】:Weird filter result in PHP function 【发布时间】:2017-02-08 02:21:44 【问题描述】: public function buscarModelos($searchWord)
$returnValue = array();
$sql = "select * from tb_modelos where 1";
if(!empty($searchWord))
$sql .= " and ( agencia_modelo LIKE ? )";
$sql .= " ORDER BY nombre_modelo";
$statement = $this->conn->prepare($sql);
if (!$statement)
throw new Exception($statement->error);
if(!empty($searchWord))
$searchWord = '%' ;
$statement->bind_param("s", $searchWord );
$statement->execute();
$result = $statement->get_result();
while ($myrow = $result->fetch_assoc())
$returnValue[] = $myrow;
return $returnValue;
我需要过滤 $searchWord,在本例中为 $searchWord = TOU656。
我的问题是查询没有按我的意愿过滤。
如您所见,有一行 (#34) 将 TOU658 作为键 agencia_modelo 的值
我正在从 ios 应用程序调用 PHP 函数,我只想获取具有 TOU656 作为键 agencia_model 的值的行,但第 34 行也作为过滤结果的一部分接收。
编辑
string(87) "select * from tb_modelos where 1 and (agencia_modelo LIKE ?) ORDER BY nombre_modelo" string(1) "%" "friends":["id_modelo":34,"nombre_modelo":"CHRYSLER 200","agencia_modelo":"TOU658","id_modelo":2,"nombre_modelo":"CHRYSLER 300","agencia_modelo":"TOU656","id_modelo":7,"nombre_modelo":"CHRYSLER NEON","agencia_modelo":"TOU656","id_modelo":16,"nombre_modelo":"CHRYSLER PACIFICA","agencia_modelo":"TOU656","id_modelo":15,"nombre_modelo":"CHRYSLER城镇和国家","agencia_modelo":"TOU656","id_modelo":6,"nombre_modelo":"CHRYSLER VISION","agencia_modelo":"TOU656","id_modelo":4,"nombre_modelo": "道奇 ATOS","agencia_modelo":"TOU656","id_modelo":5,"nombre_modelo":"DODGE ATTITUDE","agencia_modelo":"TOU656","id_modelo":32,"nombre_modelo": "道奇复仇者","agencia_modelo":"TOU656","id_modelo":13,"nombre_modelo":"DODGE DURANGO","agencia_modelo":"TOU656","id_modelo":14,"nombre_modelo": "道奇大篷车","agencia_modelo":"TOU656","id_modelo":8,"nombre_modelo":"做DGE 旅程","agencia_modelo":"TOU656","id_modelo":25,"nombre_modelo":"FIAT 500","agencia_modelo":"TOU656","id_modelo":26,"nombre_modelo":"菲亚特 500 L","agencia_modelo":"TOU656","id_modelo":27,"nombre_modelo":"FIAT 500 X","agencia_modelo":"TOU656","id_modelo":29,"nombre_modelo" :"FIAT DUCATO","agencia_modelo":"TOU656","id_modelo":21,"nombre_modelo":"FIAT MOBI","agencia_modelo":"TOU656","id_modelo":23,"nombre_modelo" :"FIAT PALIO","agencia_modelo":"TOU656","id_modelo":24,"nombre_modelo":"FIAT PALIO ADVENTURE","agencia_modelo":"TOU656","id_modelo":22,"nombre_modelo ":"FIAT UNO","agencia_modelo":"TOU656","id_modelo":11,"nombre_modelo":"JEEP CHEROKEE","agencia_modelo":"TOU656","id_modelo":10,"nombre_modelo ":"JEEP 指南针","agencia_modelo":"TOU656","id_modelo":12,"nombre_modelo":"JEEP 大切诺基","agencia_modelo":"TOU656","id_modelo":9," nombre_modelo":"JEEP PATRIOT","agencia_modelo":"TOU656","id_modelo":31,"nombre_modelo":"JEEP RENEGADE","agencia_modelo":"TOU656", "id_modelo":30,"nombre_modelo":"JEEP WRANGLER","agencia_modelo":"TOU656","id_modelo":18,"nombre_modelo":"RAM 700","agencia_modelo":"TOU656", "id_modelo":17,"nombre_modelo":"RAM PICKUP","agencia_modelo":"TOU656","id_modelo":19,"nombre_modelo":"RAM PROMASTER","agencia_modelo":"TOU656", "id_modelo":20,"nombre_modelo":"RAM PROMASTER RAPID","agencia_modelo":"TOU656"]
【问题讨论】:
看起来您正在将$searchWord
更改为%
,这将匹配任何内容。 $searchWord = '%' ;
如果你删除它,它会起作用吗?
@drew010,谢谢,我试试
如果参数$searchWord
为空,则查询结果不过滤。调用函数时确保$searchWord
不为空?
@drew010,删除那行代码也不起作用
@CalosKao,是的,我敢肯定,如果 PHP 文件没有价值,应用程序不会调用它
【参考方案1】:
如果参数$searchWord
不为空,设置值'%'
?
如果agencia_modelo LIKE '%'
,查询结果不是按agencia_modelo
列过滤的。
if(!empty($searchWord))
$searchWord = '%' ; // <--- Here
$statement->bind_param("s", $searchWord );
【讨论】:
现在如你所说,变化在哪里? @mvasco 尝试删除$searchWord = '%' ;
?以上是关于PHP函数中的奇怪过滤结果的主要内容,如果未能解决你的问题,请参考以下文章