PDO does not execute query

0

I have a form that does a search via PDO, but when I run it, it does not return anything to me.

$PDO = db_connect();

$busca = $_POST['usuario'];
$vbusca = "%".$busca."%";

$sql = 'SELECT nome, tipo, cor FROM livro AS t WHERE ';

var_dump($sql);
$stmt = $PDO->prepare($sql);
$stmt->execute([$vbusca]);
$total = $stmt->rowCount();

I have given var_dump to variable $stmt and it is returning the following:

  

string (57) "SELECT name, type, color FROM book AS t WHERE"   object (PDOStatement) # 2 (1) {["queryString"] = > string (57) "SELECT   name, type, color FROM book AS t WHERE "}

    
asked by anonymous 17.12.2018 / 16:59

1 answer

0

You need to define the field that will receive the search post, right after the WHERE clause. I will take as an example a search for the name of the book. See your complete code, where I removed its vbusca field, not necessary, and adding the $busca variable that receives $_POST directly into the new $params variable. So you can understand based on your code in the content of your question.

$PDO = db_connect();

$busca = $_POST['usuario'];
$params = array("%$busca%");

// Busca pelo nome do livro
$sql = 'SELECT nome, tipo, cor FROM livro WHERE nome LIKE ?';

var_dump($sql);


$stmt = $PDO->prepare($sql);
$stmt->execute($params);

$total = $stmt->rowCount();
    
17.12.2018 / 17:12