I am writing SQL Query based on array values but I am getting the wrong result.
For Example if my array values are ['PAK','USA'] then Query should be:-
Select * From `search` WHERE country = 'PAK' AND country = 'USA'
and if array values are ['USA','China'] then Query should be:-
Select * From `search` WHERE country = 'USA' AND country = 'China'
and so on..
I am doing this but I am getting the wrong result like this:-
SELECT * FROM `search` WHERE country = '' AND country = 'Pakistan' AND country = 'United States'
I am wondering why country = '' is always adding at the beginning of Query.. This is why I am getting the wrong result. Please help me out. Thanks in advance.. Below is my code..
PHP
$query_parts = array();
foreach($_POST['countryArray'] as $array){
$query_parts[] = "'".$array."'";
}
$string = implode(' AND country = ', $query_parts);
//echo $string;
$query = "SELECT * FROM `search` WHERE country = {$string}";
echo $query;