MYSQLI - WHERE IN array(MYSQLI - 数组中的位置)
问题描述
我在互联网上到处寻找有关此问题的答案,然后出现了准备好的语句和绑定参数(我不知道那是什么东西)
基本上,我有一个逗号分隔的列表
$list = '食物,饮料,烹饪';
好的,现在我想在数据库的一列中搜索这些项目中的每一项......听起来很简单,对吧?
$query = "SELECT * FROM table WHERE stuff IN ('$list')";$runquery = mysqli_query($connection, $query);while($row = mysqli_fetch_array($runquery,MYSQLI_ASSOC)){$variable = $row;}
然后,
var_dump($variable);
<块引用>
未定义变量
为什么?我看不出代码有什么问题.如果我输入一个特定的值,它会起作用,并且我已经使用 WHERE stuff=$item
对其进行了测试 - 效果很好.
所以不是变量/数据库,而是IN语句中的错误.我不明白为什么它不起作用.
每个元素都需要用引号括起来
$list = "'食物', '饮料', '烹饪'";$query = "SELECT * FROM table WHERE stuff IN ($list)";
或者如果你有一个数组
$array = array("食物","饮料","烹饪");$query = "SELECT * FROM table WHERE stuff IN (".implode(',', $array).")";
I've looked all over the internet for answers on this one, and prepared statements and bind params come up (I have no idea what that stuff is)
Basically, I have a comma separated list
$list = 'food, drink, cooking';
Ok, now I want to search for each of those items in a column of the database... Sounds simple, right?
$query = "SELECT * FROM table WHERE stuff IN ('$list')";
$runquery = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($runquery,MYSQLI_ASSOC)){
$variable = $row;
}
Then later on,
var_dump($variable);
UNDEFINED VARIABLE
Why? I can't see anything wrong with the code. It works if I put a particular value, and I have tested it with WHERE stuff=$item
- that works fine.
So it's not the variables / database, it's an error in the IN statement. I don't understand why it won't work.
Each element needs quotes around it
$list = "'food', 'drink', 'cooking'";
$query = "SELECT * FROM table WHERE stuff IN ($list)";
Or if you had an array
$array = array("food","drink","cooking");
$query = "SELECT * FROM table WHERE stuff IN (".implode(',', $array).")";
这篇关于MYSQLI - 数组中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MYSQLI - 数组中的位置
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01