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

2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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
@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 ?
The problem is that your values in $country are strings, but they are not in quotes in your SQL.
i have edited the answer to include the quotes, give it a shot :)
It doesn't work, but I have edited the question and added the code for the query of an only country, which works
|
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

7 Comments

why not just $countries = "'".implode("','", $country)."'"; ?
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
@RobertaGimenez please update your question to show the result of this code: echo "<pre>"; print_r($country); echo "</pre>"; We need to see the content of your array
@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
the implode is like " ' , ' " not like " , " so i don't get your comment
|
0

use IN

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

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

4 Comments

i guess your array need to be implode then
I get: Notice: Array to string conversion in C:\xampp\htdocs\food_values.php on line 24 0 Rows I don't know where is the error
your array must be like : 'india', 'canada'
But if the problem is the quotes, why does it works doing the request for an only country?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.