I want to query several words independently in mysql

Asked

Viewed 80 times

0

Good morning I have an input that generates a variable $phrase with his dice.

$phrase = "%". $phrase."%";

In php Mysql I refer to sql:

$comandoSql = select id from items where descricao like ?;

$stmt = $conexao->prepare($comandoSql) or trigger_error("triggererror ".$conexao->error);
$stmt->bind_param('s',$frase);
$stmt->execute()

If I type "notebook font", You will find "%notebook font%"

I want that when I type "notebook font", Search for: "%source%" and "%notebook%"

If it is necessary I take the bind_params, I know I would have to explode and separate in a vector, I accept this code too, reformulating my sql query and taking the bind_params.

But if you can use the bind, it would be better.

It’s like by bind_params ?

If not, what is the same variable explode code and what do I put to close the command ?

1 answer

1


Try this code, I made it quick but it’s over..

<?php
$comandoSql = "select id from items where descricao" ;

$fraseAux = split(" ", str_replace('%', '', $frase));
for($i = 0; i < sizeOf($fraseAux); $i++){
    $comandoSql .= "like ?";
    if($i < sizeOf($fraseAux)){
        $comandoSql .= " OR ";
    }
}

$stmt = $conexao->prepare($comandoSql) or trigger_error("triggererror ".$conexao->error);

for($i = 0; i < sizeOf($fraseAux); $i++){
    $stmt->bind_param('s', '%'.$fraseAux[$i].'%');
}

$stmt->execute()

Browser other questions tagged

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