By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,967 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,967 IT Pros & Developers. It's quick & easy.

multiple search

P: 28
Hi! I'm trying to do a multiple search in my php code. I have an input text field where I input my wanted values then when I click the search button, An html table would show all my searched values from my database table.

For example, I want to search multiple names(mark, jake, john, michael) <-- this is typed in a single text field separated by commas.

Then it will go to another page which will show all records from my database having a name of "mark", "jake", "john", or "michael".


This is my initial code (page afrer clicking the search button) result.php:

Expand|Select|Wrap|Line Numbers
  1. $search = $_POST["search"];
  2. $array_search = explode(',', $search);
  3. $imploded_search = implode(",", $array_search);
  4.  
  5. $query = mysql_query("SELECT * FROM users WHERE name IN($imploded_search)");
  6.  
  7. while ($row = mysql_fetch_array($query)) 
  8. {
  9.     echo $row['name'];
  10. }
  11.  
Nothing seems to display :(
When I added '' in $imploded_search in
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM users WHERE name IN('$imploded_search')"); 
It only searches for ONE name which is the first name I have inputted. ("mark")

I believe that $imploded_search creates one string 'mark, jake, john, michael' =
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM users WHERE name IN('mark, jake, john, michael')"); 
and it needs to become 'mark', 'jake', 'john', 'michael' in order for it to work.
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM users WHERE name IN('mark', 'jake', 'john', 'michael')"); 

I need help on how to do this.
THANK YOU VERY MUCH in advance!
Apr 27 '12 #1
Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
as you have already found out, you need to enclose each element of the array by quotes. why not using implode() for that (as implode() also adds the commas)?

besides the fact that you’re wide open to SQL Injection ...
Apr 27 '12 #2

P: 21
Create a simple array that can store a search word in array after parsing the special seperator that you have place like (, or ; etc)
then make a search in a loop one after another simultaneously
Apr 29 '12 #3

P: 28
Hello guys thanks for the replies,

@Dormilich - That's what I'm actually trying to do. But unfortunately, I cannot figure out how can I enclose each element of the array by quotes using implode. Please teach me how to do that. Sorry for being new to PHP functions.

@roshan ban - Are you suggesting to make a loop of SQL querries? I believe that it would only make the system slower?
Apr 30 '12 #4

P: 92
Try this:

Expand|Select|Wrap|Line Numbers
  1. $array_search = explode(',', $search);
  2. $imploded_search = "'".implode("','", $array_search)."'";
Apr 30 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.