在这里,我用,
分解了上面的字符串,得到了数组$ myArray。
<?php
$myString = '1,2,3,4';
$myArray = explode(',', $myString);
print_r($myArray);
但我怎样才能使它成为一个选择陈述
mysqli_query($con,"SELECT * FROM Persons where id = 'First part of Array'");
mysqli_query($con,"SELECT * FROM Persons where id = 'Second part of Array'");
mysqli_query($con,"SELECT * FROM Persons where id = 'Third part of Array'"); ..
在Foreach循环中
答案 0 :(得分:1)
无需爆炸。使用IN
运算符
mysqli_query($con,"SELECT * FROM Persons where id IN (".$myString.")");
查询
SELECT * FROM Persons where id IN (1,2,3,4);
答案 1 :(得分:1)
如果您携带 IN 运算符,为什么要写多个sqls:
$myArray = explode(',', $myString);
如果您对字符串值有特定需求,请使用:
$myArray =array_map('strval', $myArray );
mysqli_query($con,"SELECT * FROM Persons where id IN (".$myString.")");
答案 2 :(得分:1)
我认为对每次迭代进行查询都是个坏主意。而是运行像
这样的单个查询$myString = '1,2,3,4';
$myArray = explode(',', $myString);
// Just format the string so that it appears as '1','2'... instead of '1,2'
for ($i = 0; $i < count($myArray); $i ++)
{
$myArray[$i] = "'" . $myArray[$i] . "'";
}
$myFormattedString = implode(',', $myArray);
mysqli_query($con,"SELECT * FROM Persons where id IN (".$myFormattedString.")");
答案 3 :(得分:0)
您可以使用:
mysqli_query($con,"SELECT * FROM Persons where IN ('".$myString."')");
或者:
$ids = join(',',$myArray);
mysqli_query($con,"SELECT * FROM Persons where IN ($ids)");