0

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
$query=$_GET['Searchquery'];

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



//print_r for testing
print_r($querystring);

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



if($result=$con->query($sql))
{
$word;

//Check if query result is one or multiple entries
  if($result->num_rows==1)
$word="entry";
else
$word="entries";


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

  while($row=$result->fetch_assoc())
  {


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





  }



}
//______________________________
else {
  echo($con->error);

}






 ?>
2
  • I know. That's why I made the comment 'That's the problem' above of it. I don't know what to write. It's just a placeholder Commented Apr 15, 2016 at 20:15
  • sorry, i read the code beofer reading the actual question :p.. see if my answer helps.. don't forget you need a coliumn name after the "where" Commented Apr 15, 2016 at 20:17

1 Answer 1

2
$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:

item1','item2','item3

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.

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

3 Comments

Code-only answers are usually not really helpful. It's better to explain what you did and why, so the OP can actually learn something from it, instead of just copy/pasting it and be on his way.
The code works. But could you explain what does the code mean excactly? Because I wanted an answer how to do that, not just the code. It's still nice that you answered and I'm really thankful. But i can't really learn from that.
Sure thing, added more info.

Your Answer

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