Problem concatenating query in PDO

Asked

Viewed 183 times

3

Good night,

I have a research that contains multiple choices for checkbox and Selects and I’m trying to concatenate query to search according to the user’s choice but I do not know what I may be doing wrong I will leave the code I currently have to see and if possible tell me what I may be doing wrong.

Code

 $result_keywords = "";

    if(isset($_GET['pesquisa'])){

        $sql = "SELECT * FROM estabelecimentos WHERE keywords_pesquisa LIKE :keywords_pesquisa OR titulo LIKE :titulo AND activo = :activo ";
        $result_keywords = $conexao->prepare($sql); 

    }

    $checado = implode(",", $_POST['servicos']);
    if(isset($_POST['servicos'])){

        $sql .= "AND servicos = :servicos ";
        $result_keywords = $conexao->prepare($sql); 

    }

    if(isset($_GET['pesquisa'])){

    $result_keywords->bindValue(':activo', 1, PDO::PARAM_INT); 
    $result_keywords->bindValue(':titulo', "%{$_GET['pesquisa']}%", PDO::PARAM_STR); 
    $result_keywords->bindValue(':keywords_pesquisa', "%{$_GET['pesquisa']}%", PDO::PARAM_STR);  
    }
    if(isset($_POST['servicos'])){

        $result_keywords->bindValue(':servicos', implode(",", $_POST['servicos']), PDO::PARAM_STR); 

    }
$result_keywords->execute(); 
$row_keywords = $result_keywords->fetchAll(PDO::FETCH_ASSOC);   
  • Does an error appear? sql statement comes mounted wrong?

  • Now no error but does not present the results, the query is well mounted

  • You can put a query here in the comments

  • I already managed to list it had some syntax errors only if selecting a chekbox if selecting two does not return me data

  • For each value you need a placeholder(:palceholder) in consultation.

1 answer

1

Missing parentheses in query. Try this:

$sql = "SELECT * FROM estabelecimentos WHERE keywords_pesquisa LIKE (:keywords_pesquisa)
        OR titulo LIKE (:titulo) AND activo = :activo ";

Don’t forget to check if all the terms of your search on keywords_pesquisa and titulo are separated by a comma.

  • brackets or quotation marks ?

  • Parentheses! Quotes are "inlaid" by PDO.

Browser other questions tagged

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