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