简体   繁体   English

MySQL:如何在MySQL的WHERE IN子句中使用爆破数组?

[英]MySQL: How to use implode array in WHERE IN clause of MySQL?

I'm having a problem when I try to use implode array in WHERE IN clause and display it. 当我尝试在WHERE IN子句中使用爆破数组并显示它时,我遇到了问题。 Some of the record in the database has an apostrophe in it. 数据库中的某些记录中带有撇号。 Example the data in my database are testing , test'ing , tes't . 例如在我的数据库中的数据进行testingtest'ingtes't And my code below is how I implode the data to be used in WHERE IN clause. 下面的代码是我如何分解要在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);
?>

And here is my sample code on how I used it on WHERE IN clause. 这是我在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;
  }
?>

The output should be 输出应为

testing 测试
test'ing 测试
tes't 测试

but instead the output is just the testing . 但是输出只是testing

Check for below code 检查以下代码

<?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);
?>

And then 接着

<?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;
  }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM