Mysql
 sql >> Datenbank >  >> RDS >> Mysql

PHP/MYSQL-Abrufname basierend auf einem anderen Kriterium

Erstens, holen Sie sich NIEMALS das Passwort aus der Datenbank, das ist einfach eine extrem schlechte Praxis.

Zweitens möchten Sie den Benutzer nur dann als korrekt akzeptieren, wenn NUR eine Zeile zurückgegeben wird.

zuletzt bindColumn ist das, wonach Sie suchen.

<?php
function verify_Username_and_Pass($un, $pwd) {
    $query = "SELECT `First Name`, Username
              FROM table
              WHERE Username = :un AND Password = :pwd";
    // Don't limit the query to only one, if there is a chance that you can
    // return multiple rows, either your code is incorrect, bad data in the database, etc...

    $stmt = $this->conn->prepare($query);
    $stmt->bindParam(':un', $un);
    $stmt->bindParam(':pwd', $pwd);
    $stmt->execute();

    // Only assume proper information if you ONLY return 1 row.
    // Something is wrong if you return more than one row...
    if ($stmt->rowCount() == 1) {
        // User exist
        $stmt->bindColumn('First Name', $firstName);
        $stmt->bindColumn('Username', $username);
        // You can now refer to the firstName and username variables.
        return true;
        $stmt->close();
    } else {
        // User doesn't exist
        return false;
        $stmt->close();
    }
}
?>

Das sollte für Sie funktionieren.