Search without returning PDO data

1

My variable is being passed but in the filter process it is not always returning anything.

try 
{
   $keyword = trim($_GET["keyword"]);
   if (!empty($keyword)) 
   {

       $sql = "SELECT * FROM noticias WHERE 
         titulo LIKE :keyword OR conteudo LIKE :keyword ORDER BY Nid";

       $stmt = $DB->prepare($sql);

       $likekeyword = "%".$keyword."%";
       $stmt->bindParam(':keyword', $likekeyword, PDO::PARAM_STR);

   } 
   else 
   {
       $sql = "SELECT * FROM noticias WHERE 1 ORDER BY Nid ";
       $stmt = $DB->prepare($sql);
   }

   $stmt->execute();

I tried (titulo LIKE :termo) OR ...

Updated the strange and that the result of the if empty in white is working which leads me to believe that it is in the same query part

    
asked by anonymous 07.10.2015 / 22:14

2 answers

2

I think your problem solves itself this way:

try {
     $termo = trim($_GET["termo"]);
      if (!empty($termo)) {
$sql = "SELECT * FROM noticias WHERE titulo LIKE :termo OR conteudo LIKE :termo ORDER BY Nid";
$stmt = $DB->prepare($sql);

$likeTermo = "%".$termo."%";
$stmt->bindParam(':termo', $likeTermo, PDO::PARAM_STR);
...

Consider these differences:

  

PDOStatement::bindValue() - > The variable is bound to the reference and will only be evaluated when PDOStatement::execute() is called. And in this case, only values are accepted as arguments.

If you use $stmt->bindValue(":termo", $termo); .
The value must exist, otherwise an error will occur.

  

PDOStatement::bindParam() - > In this case, the expected argument is a reference (variable or constant) and can not be a primitive type such as a string or loose number, function return, or method.

Invalid examples:

$stmt->bindParam(':termo', 10); // Inválido
$stmt->bindParam(':termo', getValue()); // Inválido
    
07.10.2015 / 22:39
1

Dear friend, I do not know if there are any more errors, but, from this piece of code that you have, I see only one irregularity that can cause this problem, which is the number of parameters linked through bindParam >. In your SQL query you were very specific providing two placeholders , title and : content , but during the part where you linked the values of these placeholders , you passed only one.

Here are some of the fixes I applied to your script:

Example:

<?php

// Valores Extrnos
$key = isset($_GET["keyword"]) ? trim( (string) $_GET["keyword"]) : NULL;

// Conectar PDO
try{

    $opc = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
    $pdo = new PDO("mysql:hostname=localhost;dbname=banco_de_dados;", "root", "", $opc);    

    //$sql = "SELECT * FROM noticias WHERE titulo LIKE :titulo OR conteudo LIKE :conteudo ORDER BY id ASC";
    $sql = "SELECT * FROM noticias WHERE titulo LIKE :titulo OR conteudo LIKE :conteudo ORDER BY Nid";
    $stmt = $pdo->prepare($sql);

    $key = "%" . $key . "%";    

    $stmt->bindParam(":titulo", $key, PDO::PARAM_STR);  
    $stmt->bindParam(":conteudo", $key, PDO::PARAM_STR);
    $stmt->execute();

    // Capturar exceção, se haver uma
    } catch(PDOException $e){               
    echo "Erro: " . $e->getMessage();   
}

// Resultados
if(isset($stmt)){
    while($linhas = $stmt->fetch(PDO::FETCH_OBJ)){
        echo  $linhas->titulo . "<br/>";    
        echo  $linhas->conteudo. "<br/>";
    }   
}

?>

NOTE: Working with PDO requires some care, and I also noticed that in this part of your script you have try , but I did not see the part catch if you catch the exception, if any.

References:

PDO :: bindParam - PHP.net

Exceptions - PHP.net

Catch - PHP.net

    
08.10.2015 / 01:14