How to query multiple columns

0

How can I query multiple columns for the equal field value using the PDO

$PDO = db_connect();

$busca = $_POST['campo'];
$vbusca = array("%$busca%");

$sql = 'SELECT * FROM livro AS t WHERE nome LIKE ?';

$stmt = $PDO->prepare($sql);
$stmt->execute($vbusca);
$total = $stmt->rowCount();
    
asked by anonymous 17.12.2018 / 19:17

2 answers

0

You can not forget the parameters in $vbusca = array(); , for each additional field you need to add to the parameter. If you want to search for the same content, for example in book title or book type, then it would look like this:

$PDO = db_connect();

$busca = $_POST['campo'];
$vbusca = array("%$busca%", "%$busca%");

$sql = 'SELECT * FROM livro WHERE nome LIKE ? OR tipo LIKE ?';

$stmt = $PDO->prepare($sql);
$stmt->execute($vbusca);
$total = $stmt->rowCount();
    
18.12.2018 / 15:16
1

Only add other values to WHERE along with OR or AND

$PDO = db_connect();

$busca = $_POST['campo'];
$vbusca = array("%$busca%");

$sql = 'SELECT * FROM livro AS t WHERE coluna1 = ? or coluna2 = ?';

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

But your question did not make it clear what kind of data you're returning and what you're looking for. You could also make use of IN if it is multiple values.

$sql = 'SELECT * FROM livro AS t WHERE coluna1 IN (valor1,valor2)';
    
17.12.2018 / 19:49