問題描述
我在互聯(lián)網上到處尋找有關此問題的答案,然后出現(xiàn)了準備好的語句和綁定參數(shù)(我不知道那是什么東西)
基本上,我有一個逗號分隔的列表
$list = '食物,飲料,烹飪';
好的,現(xiàn)在我想在數(shù)據(jù)庫的一列中搜索這些項目中的每一項......聽起來很簡單,對吧?
$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
對其進行了測試 - 效果很好.
所以不是變量/數(shù)據(jù)庫,而是IN語句中的錯誤.我不明白為什么它不起作用.
每個元素都需要用引號括起來
$list = "'食物', '飲料', '烹飪'";$query = "SELECT * FROM table WHERE stuff IN ($list)";
或者如果你有一個數(shù)組
$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 - 數(shù)組中的位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!