首页 / PHP / php – 使用IN指令搜索预准备语句
php – 使用IN指令搜索预准备语句
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用IN指令搜索预准备语句,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2798字,纯文字阅读大概需要4分钟。
内容图文
![php – 使用IN指令搜索预准备语句](/upload/InfoBanner/zyjiaocheng/696/9e35b6d8c0aa4331a90ed4aa7a3dfc40.jpg)
我的目的是找到表中与存储在String中的集合匹配的所有项:
$array=array("item1","item2","item3","item4");//This is dynamically filled, this is just an example
$in_list = "'".implode("','",$array)."'";//that's why i use implode
$stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type IN ('.$in_list.')');
$stmt->bind_param("s", $in_list);
$stmt->execute();
$stmt->bind_result($libelle,$activite,$adresse,$tel,$lat,$lng);
这似乎不能很好地完成,它总是向我发出以下警告:
mysqli_stmt::bind_param() [<a href='mysqli-stmt.bind-param'>mysqli-stmt.bind-param</a>]: Number of elements in type definition string doesn't match number of bind variables in <b>/homepages/25/d399726988/htdocs/
如果我有一个参数,那很明显:
$stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type =?');
$stmt->bind_param("s", $parameter);
但是因为我必须处理这个问题,所以对我来说有点复杂.
解决方法:
预准备语句没有参数,因为您在准备之前已将列表插入到语句中.
$array=array("item1","item2","item3","item4");
//This is dynamically filled, this is just an example
$in_list = "'".implode("','",$array)."'";//that's why i use implode
$stmt = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements where type IN ('.$in_list.')');
此时,您创建的SQL语句是:
SELECT libelle,activite,adresse,tel,lat,lng
FROM etablissements where type IN ('item1','Item2','Item3','Item4')
由于该语句没有参数,因此mysqli_stmt :: bind_param失败.不是将项插入到语句中(易于注入),而是插入一串参数,然后绑定值(必须保持独立).
$array=array("item1","item2","item3","item4");
if (count($in_list) > 0) {
$query = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements WHERE type IN (' . str_repeat('?, ', count($in_list)-1) . '?)');
$args = $in_list;
array_unshift($args, str_repeat('s', count($in_list)));
call_user_func_array(array($query, 'bind_param'), $args);
$query->execute();
$query->bind_result($libelle,$activite,$adresse,$tel,$lat,$lng);
}
PDO的绑定接口更直接.
$array=array("item1","item2","item3","item4");
if (count($in_list) > 0) {
$query = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements WHERE type IN (' . str_repeat('?, ', count($in_list)-1) . '?)');
foreach ($in_list as $i => $arg) {
// query params are 1-based, so add 1 to the index
// PDO::PARAM_STR is the default type, so no need to pass 3rd arg
$query->bindValue($i+1, $arg);
}
$query->execute();
// no need to bind the result
}
实际上,使用PDO可以更简单,因为PDOStatement::execute可以获取参数值列表:
$array=array("item1","item2","item3","item4");
if (count($in_list) > 0) {
$query = $this->db->prepare('SELECT libelle,activite,adresse,tel,lat,lng FROM etablissements WHERE type IN (' . str_repeat('?, ', count($in_list)-1) . '?)');
$query->execute($in_list);
}
内容总结
以上是互联网集市为您收集整理的php – 使用IN指令搜索预准备语句全部内容,希望文章能够帮你解决php – 使用IN指令搜索预准备语句所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。