PDO does not execute query

Asked

Viewed 75 times

0

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

$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 gave one var_dump in the variable $stmt and is returning me 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" }

  • How is the connection made with the bank? And not a var_dump on $sql and yes in the $total

  • string(57) "SELECT name, type, color FROM book AS t WHERE" int(0)

  • Did not miss the field by receiving the value after the WHERE? WHERE busca = ?

  • Your select is wrong, missed the column and condition in the WHERE.

  • @Guilhermerigotti as I can do to search the 3 columns?

1 answer

0


You need to define the field that will receive the search post, right after the clause WHERE. I will take a search for the name of the book. See your full code, where I removed your field vbusca, not needed and adding the variable $busca who receives the $_POST directly in the new variable $params. 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();
  • Is returning me so now string(70) "SELECT name, type, color FROM search AS t WHERE book LIKE ?" int(0)

  • But what field do you want to look for? For example, if you look for the name of the book, it would have to be: $sql = 'SELECT nome, tipo, cor FROM livro WHERE nome LIKE ?';

  • There’s no way to search all the columns?

  • @Teresasantos First of all, you have to say if this has solved your problem number 1. The problem you questioned. If so, wonder! The next step would be another question. For it is a new question that you present to us. already answering beforehand. Yes, you have to search in all columns, but it would change that simple base of your code. Then, another question should be asked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.