問(wèn)題描述
我在互聯(lián)網(wǎng)上到處尋找有關(guān)此問(wèn)題的答案,然后出現(xiàn)了準(zhǔn)備好的語(yǔ)句和綁定參數(shù)(我不知道那是什么東西)
基本上,我有一個(gè)逗號(hào)分隔的列表
$list = '食物,飲料,烹飪';
好的,現(xiàn)在我想在數(shù)據(jù)庫(kù)的一列中搜索這些項(xiàng)目中的每一項(xiàng)......聽(tīng)起來(lái)很簡(jiǎn)單,對(duì)吧?
$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);
<塊引用>
未定義變量
為什么?我看不出代碼有什么問(wèn)題.如果我輸入一個(gè)特定的值,它會(huì)起作用,并且我已經(jīng)使用 WHERE stuff=$item
對(duì)其進(jìn)行了測(cè)試 - 效果很好.
所以不是變量/數(shù)據(jù)庫(kù),而是IN語(yǔ)句中的錯(cuò)誤.我不明白為什么它不起作用.
每個(gè)元素都需要用引號(hào)括起來(lái)
$list = "'食物', '飲料', '烹飪'";$query = "SELECT * FROM table WHERE stuff IN ($list)";
或者如果你有一個(gè)數(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).")";
這篇關(guān)于MYSQLI - 數(shù)組中的位置的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!