Use PHP PDO for search

Asked

Viewed 47 times

-4

Where is the error in my code. I do not know why it does not return the search result.

<?php
require_once('db_connect.php');
$conectar = new 
PDO("mysql:host=$host;bancodedados=$bancodedados;charset=utf8", 
$usuario, $senha);

$nomeProduto = $_POST['nomeProduto'];
$sql = "SELECT * FROM nilo_db.nilo_tb WHERE nomeProduto = 
:nomeProduto";
$queryResult = $conectar->prepare($sql);
$queryExec = $queryResult- 
>execute(array(":nomeProduto"=>$nomeProduto));
if($queryExec){
    if($queryResult->rowCount()>0){
        foreach($queryResult as $row){ ?>
            <tr>
                <td class="col">
                    <?php echo $row['nomeProduto'] ?>
                    <?php echo $row['codigoProduto'] ?>
                </td>
            </tr>
        <?php }
       }
    }
?>
  • Click [Edit] and put the last lines of the PHP bug log that makes it easy for us to help. There’s a line break in -> of the execute, but I imagine this was when pasting the code here?

  • Yes, it was when putting the execute here, however, the problem is that it does not return any error.

  • In the error log always appears (if it is a mistake, it can just happen that your query is not returning anything), so I asked to add.

  • I have noticed that you have already asked several questions but I also noticed that you have not marked any as accepted. It is good practice to mark an answer as accepted, as in https://i.stack.Imgur.com/evLUR.png and because in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079.

1 answer

0

  • In connection string change bancodedados=$bancodedados for dbname=$bancodedados
  • If that wasn’t by pasting the code here, hit that line of code

    $queryExec = $queryResult- 
    >execute(array(":nomeProduto"=>$nomeProduto));
    

    to avoid errors of the type PHP Parse error: syntax error, unexpected '>' in ....

    The correct is no line break -> and without spaces

    $queryExec = $queryResult->execute(array(":nomeProduto"=>$nomeProduto));

Corrected code

    $conectar = new PDO("mysql:host=$host;dbname=$bancodedados;charset=utf8", $usuario, $senha);

    $nomeProduto = $_POST['nomeProduto'];
    $sql = "SELECT * FROM nilo_db.nilo_tb WHERE nomeProduto = :nomeProduto";

    $queryResult = $conectar->prepare($sql);
    $queryExec = $queryResult->execute(array(":nomeProduto"=>$nomeProduto));
    if($queryExec){
        if($queryResult->rowCount()>0){
            foreach($queryResult as $row){ ?>
                <tr>
                    <td class="col">
                        <?php echo $row['nomeProduto'] ?>
                        <?php echo $row['codigoProduto'] ?>
                    </td>
                </tr>
            <?php }
           }
        }

How to connect a Mysql database using PHP

Using PDO

$conectar = new PDO("mysql:host=HOST;dbname=NOME_DB", "USUARIO", "SENHA");
  • HOST: Host connection to the database;
  • USER: Access user to the connection database;
  • PASSWORD: Password to the database specified on the connection;
  • NOME_DB: Name of the base you want to access.

Browser other questions tagged

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