PDO does not return query results

Asked

Viewed 66 times

-1

I have a code where you would have a select to select in which column you want to search and click on the letter to search within the selected column the selected letter.

The error is on my button. It would have to be a <a href=""> only that by having a <form action=""> directed to another page the href conflicts with the form

Search code letter "A":

require_once 'functions.php';

    $PDO = db_connect();

    $opcao_filtro = filter_input(INPUT_POST, 'opcao_filtro', FILTER_SANITIZE_STRING);

    $sql = 'SELECT * FROM tablet AS t WHERE';

  //idlivro, titulo, tipo, e cor.
  switch($opcao_filtro)
  {
      switch($opcao_filtro)
{

    case 'titulo': {
        $sql .= ' t.titulo like "a%"';
        break;
    }
    case 'cor': {
        $sql .= ' t.cor like "a%"';
        break;
    }
    case 'categoria': {
        $sql .= ' t.categoria like "a%"';
        break;
    }
}


    $stmt = $PDO->prepare($sql);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $result = array_unique($rows);

        foreach($rows as $row)
        {
            echo $row['titulo'];
            echo $row['cor'];
            echo $row['categoria'];
        }

Button tag:

<button type="submit" class="btn btn-link btn-xs" value="a" name="a">A</button>

Select:

<div class="op">
    <select name="opcao_filtro" class="form-control" style="width:150px; height:30px;">
        <option value="nulo">---</option>                
        <option value="titulo">Título</option>
        <option value="autor">Autor</option>
        <option value="tema">Tema</option>
        <option value="editora">Editora</option>                                    
    </select>
</div>
  • To know what is happening, you can give a print_r($stmt->errorInfo()) shortly after the $stmt = $PDO->prepare($sql)

  • Even so it returns blank.

  • I realize you don’t have the method to execute: $stmt->execute() which would be shortly after the $stmt = $PDO->prepare($sql)

  • I added it and it didn’t work :/

  • Add these lines here at the beginning of the file and update to see

  • Returned this error Uncaught Pdoexception: could not find driver in

Show 1 more comment

1 answer

2

The assembly of your WHERE is incorrect, the sign ?(query), will be replaced by the parameter passed, then the WHERE should be assembled as follows:

switch($opcao_filtro)
{
    case 'titulo': {
        $sql .= ' t.titulo like "?%"';
        break;
    }
    case 'autor': {
        $sql .= ' t.cor like "?%"';
        break;
    }
    case 'tema': {
        $sql .= ' t.categoria like "?%"';
        break;
    }
}

Another important point to note is that either you use the =(equal) to compare integer values, or you use the LIKE with %(percentage) for partial comparison.

  • or use bindValue and use :valor in the WHERE

  • I thought of that possibility, but if I’m not mistaken, bindValue by default uses the type PDO::PARAM_STR, then the contents would be enclosed in quotation marks.

  • Quotes would be in the general string $sql .= " t.titulo LIKE :titulo";

  • No, the quotation marks should be only in the value entered by the user and in %

  • If you were to use bindValue, you could use it like this: $stmt->bindValue(':titulo', $titulo) and initialize the variable with so: $titulo = "%valor" who would also work

Browser other questions tagged

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