sql >> Base de Datos >  >> RDS >> Mysql

PHP/MYSQL Recuperar nombre basado en otro criterio

En primer lugar, NUNCA tome la contraseña de la base de datos, eso es una práctica extremadamente mala.

En segundo lugar, solo desea aceptar al usuario como correcto si SOLO se devuelve una fila.

por último bindColumn es lo que estás buscando.

<?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();
    }
}
?>

Eso debería funcionar para ti.