Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

I am sharing the script down below which I created for login:

function login($sql) {

try {                   

    $fbhost = "localhost";

    $fbname = "foodbank";

    $fbusername = "root";

    $fbpassword = "";

    $DBH = new PDO("mysql:host=$fbhost;dbname=$fbname",$fbusername,$fbpassword);

    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $STH = $DBH->query($sql);       

    $STH->setFetchMode(PDO::FETCH_ASSOC);

    session_start();        

    if ($row = $STH->fetch()) {

        $_SESSION['username'] = "$row[username]";

        header("Location:index.php");   

    }

} catch(PDOException $e) {

    echo $e->getMessage();

}

}

1 Answer

0 votes
by (11.7k points)

Well, I think as your function is named as login, you need to have something like $sql: “SELECT password FROM users WHERE username = :username”, then looping over the result and login user if it works.

If you want to get more insights into SQL, check out this SQL Course from Intellipaat.

Browse Categories

...