4

I'm trying to pull multiple rows from a single table. I'm trying to pull either all males or all females in different zip codes.

<?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...
}
?>
BRHETT
  • 55
  • 1
  • 3

2 Answers2

2

You should use IN on this,

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

currently your code is vulnerable to SQL Injection. Please read on PDO or MySQLI extension.

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

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0
// Prevent SQL injection for user input
$fetchUser = mysql_query("select * from users where gender = '".filter_var($_POST[gender], FILTER_SANITIZE_STRING)."' OR zipCode IN (".implode(",", $zipCodes).") order by id desc");)
Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52