Multiple selection in one table using php mysql

I am trying to pull multiple rows from one table. I am trying to pull all men or all women into 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... } ?> 
+4
source share
2 answers

You must use IN for this,

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

Your code is currently vulnerable to SQL Injection. Please read PDO or MySQLI .

Read more about this article: the best way to prevent SQL injection in PHP
PHP PDO: can I bind an array to an IN () condition?

+2
source
 // 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");) 
0
source

Source: https://habr.com/ru/post/1436313/


All Articles