LIKE with explodes PDO

Asked

Viewed 54 times

0

Good night consecrated, I have a search page, in which I want that when someone searches something like "i3 memory screen cabinet" appear all the items available in the table with respect to these searched items, for this I must use the explode in the variable that recovers the values and put in the LIKE, the problem is there, it creates an array, that I have no way of knowing how many values will have depending on the user’s search, if I put LIKE 'variable[0]' it goes and even without the 0, but returns only the first entered value, I wonder if have a way to pull others so that does not pull the whole table due to an empty array. Follow the Code:

<?php        
$buscaexp = explode(" ",$buscanaurl);
$procura = $conn->prepare("SELECT * FROM produtos WHERE nome LIKE '%$buscaexp[0]%' OR (nome LIKE '%$buscanaurl%')");
    $procura->execute();
?>

follows link to anyone who wants to look http://www.tcc2016.96.lt/busca?busca=memoria+i3

  • The duplicate links somehow helped me a little, however I still could not make it work, maybe because PDO needs something that I still do not know what it is

  • is like this: for($i = 0; $i < Count($buscaexp); $i++) $param = "LIKE name '%". $buscaexp[$i]." %'"; //var_dump($buscaexp); $procura = $Conn->prepare("SELECT * FROM products WHERE ". $param); $procura->execute();var_dump($procura);

  • and in the var_dump output this way: Object(Pdostatement)#1 (1) { ["queryString"]=> string(45) "SELECT * FROM products WHERE NAME LIKE '%i3%'" } even when I search for more than one product in the bar is as if it only searches for the last array

1 answer

1


Hi

Try to check if there is value at position [0]

isset(array[0])

true case, then add in query string

  • so friend my biggest problem is in the 0 of $buscaexp[0] because there ta specifying q is to query only the first string, I need something identify how many strings have and do the search in each of them

  • yes there is value in position 0, if I put "memory ddr2" there will be in position 0 and 1, what I need is to show results related to both 0 and 1 and so on if there is

  • isset worked thanks a lot for the tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.