PHP - Return database value - specific value

Asked

Viewed 35 times

0

need a light to return only a specific value according to the login done.

Example: if the database username is "Amora" returns the value of the column Name whose username is Amora.

I believe it is related to SELECT but I do not know how to do if you can give me some hint.

    <?php
require_once 'config.php';

try {
    

    $sql = 'SELECT username,
                    password,
                    Nome
               FROM login
              ORDER BY Nome';

    $q = $con->query($sql);
     $q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
   die("Could not connect to the database $database :" . $e->getMessage());
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>WMS - Lear Betim</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>
        <div id="container">
            
            <table class="table table-bordered table-condensed">
              
                <tbody>
                    <?php while ($row = $q->fetch()): ?>
                        <tr>
                           <td> <?php echo htmlspecialchars('Olá' . ", " . $row['Nome']); ?></td> 
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
    </body>
</div>
</html>
  • 1

    If to return the record of the login table where Name = Amora select columns from login Where Name = 'Amora' ....

  • Thank you for your comment helped me to solve the problem in question. I used $sql = "SELECT username, password, Name FROM login WHERE username = 'amora'";

1 answer

1


It is necessary to include the WHERE clause in the SQL database query.

The SQL WHERE clause

The clause WHERE is used to filter records. It is used to extract only records that meet a specified condition. Source: link.

Syntax WHERE:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Condititon can be, for example: WHERE Nome = 'Amora'. It may have more than one condition, such as: WHERE username = 'Amora_Amora' AND password = '123456'. It is also possible to use other logical and conditional operators (example link1 and Link2), among them: =, <, >, <>, AND, OR, BETWEEN and etc.

Note: The clause WHERE is not used only in SELECT, it is also used in UPDATE, DELETE and etc.


In PHP, when using PDO, it is possible to pass this value dynamically. It is recommended to use prepare (here), this helps prevent attacks from SLQ Injection in queries that are fired from external parameters mainly, for example. It would look similar to code:

$nomeUsuario = "Amora"; //aqui seria possível mudar para um conteúdo de GET/POST, por exemplo.
$sql = "SELECT username,
               password,
               Nome
        FROM login
        WHERE Nome = :nome";
$q = $con->prepare($sql);
$q->bindParam(":nome", $nomeUsuario);
$q->execute();
$consulta = $q->fetch(PDO::FETCH_ASSOC);

Browser other questions tagged

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