MYSQLI-数组中的位置 [英] MYSQLI - WHERE IN array
问题描述
我已经在互联网上寻找了关于该问题的答案,并且准备了语句和绑定参数(我不知道那是什么东西)
基本上,我有一个用逗号分隔的列表
$list = 'food, drink, cooking';
好吧,现在我想在数据库的一列中搜索每个项目...听起来很简单,对吧?
$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 = "'food', 'drink', 'cooking'";
$query = "SELECT * FROM table WHERE stuff IN ($list)";
或者如果您有数组
$array = array("food","drink","cooking");
$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-数组中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!