Search in 2 fields at the same time

0

Eae,

I have two fields in a mysql table, description and degree.

How do I find a result when I search for WORD and 4 (WORD from the description field and 4 from the field?

PS: Both description and degree are in text mode.

PS2: if I search separately it finds result (like, if I type only WORD it finds, and if I search only 4 it thinks too)

    
asked by anonymous 17.06.2018 / 19:19

2 answers

0

Doing the explode and mounting your query:

//Separo a frase por espaço
$v = explode(" ", "palavra 4");

$query = 'SELECT * FROM sua_tabela WHERE ';

foreach ($v as $key => $value) {
   $query .= "campo_1 LIKE '%" . $value . "%' OR campo_2 LIKE '%" . $value . "%' OR ";
}

//Retiro o último 'OR '
echo substr($query, 0, -3);

The example prints: SELECT * FROM sua_tabela WHERE campo_1 LIKE '%palavra%' OR campo_2 LIKE '%palavra%' OR campo_1 LIKE '%4%' OR campo_2 LIKE '%4%'

This example is very generic, but with it you can understand how you can assemble your query very simply

    
17.06.2018 / 22:41
0

So I understand you want to do a query that searches in two fields at the same time if something is in one field or the other ... if that is it and only use OR.

If you are wanting to search for two typed words in the same input, you will have to treat them first. I do not know much about php, but it should have some function that separates a string, like a split () where you pass as a separator argument, this tab can be a space, a comma or a tab.

    
17.06.2018 / 20:55