MYSQLI - 数组中的位置

时间:2023-03-05
本文介绍了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 - 数组中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:单击按钮时更新信息表 下一篇:无法将内爆数组绑定到 mysql 准备好的语句中

相关文章