how to search for multiple values

+2 hareth muthanna · March 14, 2016
i want to search for multiple values in one column in mysql through PHP

for example i write 2 or 3 names in one text box and the code will search for them in mysql

....
thanks for anyone could help :) 

......
b the way i want to inset multiple values in the same way ... 

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Jay Deshaun · March 14, 2016
You would have to use the keyword "OR"

For an example,
I'm searching for your first name and mines in a database called accounts
"SELECT * FROM accounts WHERE firstName = 'hareth' OR firstName = 'Jaiquon'"


If you want to search for something containing the value, you would have to use the keyword LIKE

For an example,
I'm searching for someone with a similar first name as you
"SELECT * FROM accounts WHERE firstName LIKE 'har'"
+1 AJALA JOHN · March 14, 2016
You could use mysql FULLTEXT search. It's more efficient and reliable.

See Advanced text searching using full-text indexes on http://www.hackingwithphp.com/9/3/18/advanced-text-searching-using-full-text-indexes

SELECT * FROM opinions WHERE Match(Opinion) AGAINST ('nice language' IN
BOOLEAN MODE);

I hope this helps.
0 Jay Deshaun · March 15, 2016
@Ajala That seems like an interesting one, I will have to read that when I get time
0 hareth muthanna · March 16, 2016
thanks for your replys guys you where really helpful

i used the explode  method ,, it worked but with some problem 

i want to use 


explode and implode ...  so i write multiple values in a text box
and i search for them in mysql 

could you help me with this code ?


$bits = explode(' ', $search);

  $sql = "SELECT * FROM nott WHERE number LIKE '%" . implode("%' OR name LIKE '%", $bits) . "%'";
0 Jay Deshaun · March 16, 2016
$bits = explode(' ', $search);
$sqlBits = implode("%' OR name LIKE '%", $bits);
$sql = "SELECT * FROM nott WHERE number LIKE '%$sqlBits%'";


Hope this works.
+1 Jonah Morrison · March 18, 2016
Please be careful. Always santize your inputs. Never directly put a variable in a SQL string unless it has been sanitized.
0 Jay Deshaun · March 21, 2016
Yea you're right, I tend to see if the script would work first then sanitize them.
0 Jonah Morrison · March 21, 2016
Haha, all good. Probably a bad practice though.
0 paulne vinthomas · March 31, 2016
There's a decent article here which will give you a decent introduction to searching MySQL with PHP, but basically what you want to do is split your search phrase in to parts and then use them in the MySQL query. For instance:

<?php

  $search = 'Gold Chain Shirt';

  $bits = explode(' ', $search);

 

  $sql = "SELECT name FROM product WHERE name LIKE '%" . implode("%' OR name LIKE '%", $bits) . "%'";

 

The above will generate this query:

SELECT name FROM product WHERE name LIKE '%Gold%' OR name LIKE '%Chain%' OR name LIKE '%Shirt%'

 http://goo.gl/maoapk
0 paulne vinthomas · March 31, 2016
There's a decent article here which will give you a decent introduction to searching MySQL with PHP, but basically what you want to do is split your search phrase in to parts and then use them in the MySQL query. For instance:

<?php

  $search = 'Gold Chain Shirt';

  $bits = explode(' ', $search);

 

  $sql = "SELECT name FROM product WHERE name LIKE '%" . implode("%' OR name LIKE '%", $bits) . "%'";

 

The above will generate this query:

SELECT name FROM product WHERE name LIKE '%Gold%' OR name LIKE '%Chain%' OR name LIKE '%Shirt%'

 
goo.gl/maoapk
  • 1
  • 2

PHP

130,947 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators