How to use PDO and PHP to Show in Table Query Data

Asked

Viewed 28 times

0

I have a question. I do not understand why my code is not correct. Please, if you can review it, I would be very happy.

<?php
    require_once "db_connect.php";

    //conectar banco
    $conectar = new PDO("mysql:host=$host;bancodedados=$bancodedados", 
    $usuario, $senha);

    if(isset($_POST['nomeProduto'])){
        $nomeProduto = $_POST['nomeProduto'];
        $query = $conectar->prepare("SELECT * FROM nilo_db.nilo_tb
        WHERE nomeProduto LIKE ?");
        $query->execute(array($nomeProduto));
        if($query->rowCount() == 0){
            while($linha = $query->fetch()){ ?>
                <tr class="text-center">
                    <td><?php echo $linha['nomeProduto']; ?></td>
                    <td><?php echo $linha['categoriaProduto']; ?></td>
                    <td><?php echo $linha['codigoProduto']; ?></td>
                    <td><?php echo $linha['fornecedor']; ?></td>
                    <td><?php echo $linha['emEstoque']; ?></td>
                </tr>
<?php       }
        }
    }

?>
  • bancodedados=$bancodedados would not be dbname=algo.... ?

  • So, the connection is working properly, because with Insert is working well. Is that I named the database "bancodedados" same.

  • If that’s not a problem, then it should be LIKE you won’t pass the % ? can put them together in the execute(). You could better describe what the problem or symptom of the code is by knocking your eye those were the things I saw 'out of the pattern'.

No answers

Browser other questions tagged

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