使用php mysql在单个表中进行多项选择

我正试图从一个表中拉出多行.我试图用不同的邮政编码拉动所有男性或所有女性.

<?php
$zipCodes = array("55555", "66666", "77777", etc...);

$fetchUser = mysql_query("select * from users where gender = '$_POST[gender]' ".implode(" or zipCode = ", $zipCodes)." order by id desc");
while($var = mysql_fetch_array($fetchUser)) {
  code...
}
?>

最佳答案 你应该使用IN,

SELECT ...
FROM   tableName
WHERE gender = '$_POST[gender]' AND
      zipCode IN (55555, 6666, 77777)

目前您的代码容易受到SQL注入攻击.请阅读PDOMySQLI分机.

Read more on this article: Best way to prevent SQL injection in PHP
PHP PDO: Can I bind an array to an IN() condition?

点赞