Eu tenho algum código PHP que leva uma solicitação de postagem como entrada para uma instrução preparada. Não está retornando nenhuma informação. Não há erros.
Tentei codificar as variáveis que são passadas para o objeto preparado, sem sucesso.
Se eu consultar manualmente o banco de dados com a consulta desejada, a saída é recebida.
O que estou perdendo aqui? O que posso fazer para obter resultados?
Aqui está meu código:
<?php
$username = "user";
$password = "ultrasecurepassword";
try {
$pdo = new PDO('mysql:unix_socket=/run/mysql/mysql.sock;dbname=news', $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$query = "SELECT * FROM newsdb ORDER BY pubdate DESC LIMIT 250";
if(!empty($_POST['search'])){
$termobusca = htmlspecialchars($_POST['search']);
$tipobusca = htmlspecialchars($_POST['searchtype']);
if($tipobusca == "title"){
$stmt = $pdo->prepare("SELECT * from newsdb where title like '%:term%' ORDER BY pubdate DESC limit 5000;");
}
else {
$stmt = $pdo->prepare("SELECT * from newsdb where pubdate like '%:term%' ORDER BY pubdate DESC limit 5000;");
}
$stmt->bindParam(1, $termobusca);
}
else {
$stmt = $pdo->prepare("SELECT * FROM newsdb ORDER BY pubdate DESC LIMIT 250");
}
$stmt->execute();
while($row = $stmt->fetch()){
print_r($row);
}
$pdo = null;
?>
Eu já vi essa pergunta antes, de alguma forma, tente isso, por favor
if($tipobusca == "title"){
$stmt = $pdo->prepare("SELECT * from newsdb where title like :term ORDER BY pubdate DESC limit 5000;");
}
else {
$stmt = $pdo->prepare("SELECT * from newsdb where pubdate like :term ORDER BY pubdate DESC limit 5000;");
}
$term = '%'.$termobusca.'%';
$stmt->bindParam(':term', $term, PDO::PARAM_STR);
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras