Problem concatenating query in PDO

3

Good evening,

I have a search that contains multiple choices for checkbox and Selects and I'm trying to concatenate the query to search according to the user's choice but give me error I do not know what I can be doing wrong I'm going leave the code I have currently 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);   
    
asked by anonymous 25.03.2015 / 23:22

1 answer

1

There is no need to use parentheses in the query. Try this:

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

Do not forget to check that all of your search terms in keywords_pesquisa and titulo are separated by commas.

    
06.04.2015 / 20:46