为什么子句IN不能在我的代码中工作?
<?PHP
$id_user = array('aaa' , 'bbb');
$ids = join(',',$id_user);
$sql = "SELECT * FROM users WHERE username IN ($ids) order by id asc";
$result = mysql_query($sql);
$tcount = mysql_num_rows($result);
$i = 0;
$number = 0;
while($i<$tcount)
{
$number++;
mysql_data_seek($result,$i);
$datas=mysql_fetch_array($result);
{
$id = stripslashes(str_replace('\r\n', '<br>',($datas['id'])));
echo $id;
echo "<br>";
}
$i++;
$count++;
}
?>
答案 0 :(得分:0)
您的用户名是字符串,因此应该用引号括起来
$id_user = array('aaa' , 'bbb');
$ids = join(',', array_map(function($v) {
return sprintf('"%s"', mysql_real_escape_string($v));
}, $id_user));
$sql = "SELECT * FROM users WHERE username IN ($ids) order by id asc";
// here goes the rest of your code
答案 1 :(得分:0)
试试这个
$ids = join("','",$id_user);
$sql = "SELECT * FROM users WHERE username IN ('$ids') order by id asc";