Needforbleed Needforbleed - 6 months ago 10x
PHP Question

How to Query MySQL Database for PHP $_GET Array

I'm trying to create a simple search field, in which the user can write a text.
This text gets transfered to php which then accesses the text through GET.
If the text contains whitespaces it gets converted in to an array.
The problem is that I can't figure out how to query for entries with an array as the Where condition.
I tried several answers like implode(",",$array) or WHERE IN($array).
None of them worked for me.
Either I get array to string comparison errors or unkown colum errors.
The $_GET array contains the string submitted by the form, so that's not the error.

Thanks for your help!

PHP Script

//string containing the submitted searchbox value

//Seperate string where whitespaces are detected and create array holding these strings
$querystring=explode(" ",$query);
$con=new mysqli($Server,$User,$UserPassword,$Database);

//print_r for testing

//That's the problem
$sql="SELECT * FROM tb_user WHERE ??????"


//Check if query result is one or multiple entries

echo("<h1>We found ".$result->num_rows." ".$word." meeting your Keyword '".$query."'</h1>");


echo("<div id='founduser'><a href='profile.php?ProfileLink=".$row['ProfileLink']."'><h1>".$row['GivenName']." ".$row['FamilyName']."</h1></a></div>");


else {



$sql="SELECT * FROM tb_user WHERE column_name IN ('".implode("','", $querystring)."')";

implode() is the opposite of explode(). It will join all the indexes of the array with the string provided. If you extract the implode from the query you get something like this:


From there we're olny missing the outer quotes for the first and last item, so we can just add them to the query as shown above.

The query above would be generated as:

SELECT * FROM tb_user WHERE column_name IN ('item1','item2','item3')

The IN clause checks to see if any of the provided values match anythign in the given column.