4

I'm doing a MySQL query in a PHP file. I have an array ($country) with multiple names of countries and I want to select the rows of those countries from my table.

$country is the following array:

Array
(
    [0] => Afghanistan
    [1] => Armenia
    [2] => Bhutan
)

I'm using the following code:

$result = mysqli_query($con,"SELECT * 
                             FROM table1 
                             WHERE table1.country='".$country."'");

However, the following statement works for an only country:

 $result = mysqli_query($con,"SELECT * FROM table1 WHERE table1.country='".$country[1]."'");

But it doesn't work, whn I try: mysqli_num_rows() of the $result it says that the parameter is a booelan(That's because the query fails and it returns a false). Does anyone know what is the error?

This is the table1 structure:

enter image description here

3 Answers3

2

use can use IN like so:

// Array should look like this.
// $country = array('spain', 'UK', 'Germany');

$result = mysqli_query($con, "SELECT * FROM table1 WHERE table1.country IN ('". implode("' , '", $country) . "')");

PS. Don't use the array in the answer as you might get empty result.

xanadev
  • 751
  • 9
  • 26
  • I obtain $result = false but when I do the query with an only value, it works, so the problem is the array. I don't understand what is the problem – Roberta Gimenez Jun 06 '18 at 08:29
  • @RobertaGimenez first i've edited the answer to change the array name. if the $result is false it just means that your query has failed, can you share the table1's structure ? – xanadev Jun 06 '18 at 08:38
  • 1
    The problem is that your values in `$country` are strings, but they are not in quotes in your SQL. – Nigel Ren Jun 06 '18 at 08:41
  • i have edited the answer to include the quotes, give it a shot :) – xanadev Jun 06 '18 at 08:45
  • It doesn't work, but I have edited the question and added the code for the query of an only country, which works – Roberta Gimenez Jun 06 '18 at 08:53
  • @RobertaGimenez does it show any errors, include them in your question. – xanadev Jun 06 '18 at 09:02
  • I added it in the question. But the error is that the result of the query is a boolean, so it's failing – Roberta Gimenez Jun 06 '18 at 09:06
  • @MacBooc i don't think so, i've tested the code before, and the result is as expected. – xanadev Jun 06 '18 at 09:16
  • @RobertaGimenez what does var_dump($country) show, as far as i can tell the array in your question looks like an array of constants, not an array of strings. – xanadev Jun 06 '18 at 09:21
1

You cannot pass the array to the query. You can use IN to pass different, comma separated values to the query. So first you have to take each array entry and concatenate it with commas:

//initialize the list
$countries = "";
foreach($country as $a){
    //add each country wrapping it in single quotes
    $countries .= "'".$a."',";
}
//remove the last comma that is not necessary
rtrim($countries,",");
//build the query
$sql = "SELECT * FROM table1 WHERE table1.country IN ($countries)";
//run the query
$result = mysqli_query($con,$sql);

Note that building $countries I have put single quotes around each element. The reason is that I am passing strings to the database. This would not be necessary in case of integers

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • why not just `$countries = "'".implode("','", $country)."'";` ? – Frankich Jun 06 '18 at 09:08
  • It fails too and the query returns false. But if the problem are the quotes I don't understand why does it works when I do the query of an only element of the array – Roberta Gimenez Jun 06 '18 at 09:08
  • @RobertaGimenez please update your question to show the result of this code: `echo "
    "; print_r($country); echo "
    ";` We need to see the content of your array
    – Lelio Faieta Jun 06 '18 at 09:12
  • @MacBooc your code will produce a different result 'country1,country2,country3,' that will be threated as a single value by mysql while we are looking for 'country1','country2','country3'. Each country is wrapped in quotes, commas are not and there are no extra commas at the end. So each country will be a single value for IN – Lelio Faieta Jun 06 '18 at 09:14
  • the implode is like `" ' , ' "` not like `" , "` so i don't get your comment – Frankich Jun 06 '18 at 09:15
  • @LelioFaieta I put the $country structure in the question – Roberta Gimenez Jun 06 '18 at 09:20
  • `$country = ['Albania', 'Afghanistan', 'USA']; $countries = "'".implode("','", $country)."'"; echo $countries;` try it yourself you'll see – Frankich Jun 06 '18 at 09:21
0

use IN

Note that your country data like $country = ['india', 'canada'];

$result = mysqli_query($con,"SELECT * FROM table1 WHERE table1.country IN('".$country."')");
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37