当我尝试在 WHERE IN
子句中使用内爆数组并显示它时遇到问题。数据库中的某些记录中有撇号。
例如,我的数据库中的数据是 testing
、test'ing
、tes't
。下面的代码是如何内爆要在 WHERE IN
子句中使用的数据。
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
$t = mysqli_real_escape_string($con,$txt);
$arr[] = "'".$t."'";
}
$sampl = implode(',',$arr);
?>
这是我的示例代码,说明我如何在 WHERE IN
子句中使用它。
<?php
$qry2 = mysqli_query($con,"SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN (".$sampl.")")or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>
输出应该是
testing
test'ing
tes't
但输出只是测试
。
最佳答案
检查以下代码
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
//$t = mysqli_real_escape_string($con,$txt);
$arr[] = '"'.$txt.'"';
}
$sampl = implode(',',$arr);
?>
然后
<?php
$qry2 = mysqli_query($con,'SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN ('.$sampl.')')or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>
关于php - MySQL:如何在 MySQL 的 WHERE IN 子句中使用内爆数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33029550/