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

时间:2015-10-09 03:42:01

标签: php mysql

当我尝试在WHERE IN子句中使用implode数组并显示它时,我遇到了问题。数据库中的一些记录中有一个撇号。 我的数据库中的数据示例为testingtest'ingtes'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

但输出只是testing

1 个答案:

答案 0 :(得分:0)

检查以下代码

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