Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 143

Login Authentication Query in MySQL

In this lesson: Write a secure login authentication query in MySQL with PHP PDO and password_verify — full example. In Hindi & English.

Logging a user in

A login query checks a username and password against the database. Done wrong, it is one of the biggest security holes in an app. The safe way rests on two rules: never store plain passwords, and never put user input directly in SQL.

💡 Fetch the user by username with a prepared statement, then verify the password against a stored hash.

The dangerous way

-- NEVER do this
SELECT * FROM users
WHERE username = '$user' AND password = '$pass';

This has two fatal flaws: the password is compared in plain text (so it must be stored unhashed), and the user input is pasted into the SQL, allowing SQL injection. A crafted username could bypass the login entirely.

Never store plain passwords

// when the user registers (PHP)
$hash = password_hash($password, PASSWORD_DEFAULT);
// store $hash in the password column, not $password

Store a strong one-way hash, not the real password. password_hash uses bcrypt, so even if the database leaks, attackers cannot read the passwords.

Use prepared statements

// PHP with PDO
$stmt = $pdo->prepare('SELECT id, password FROM users WHERE username = ?');
$stmt->execute([$username]);
$user = $stmt->fetch();

A prepared statement sends the query and the value separately, so the username can never alter the SQL. This closes the SQL injection hole completely. Notice we fetch by username only — not the password.

Verifying the password

if ($user && password_verify($password, $user['password'])) {
    // login success
} else {
    // invalid username or password
}

password_verify compares the typed password with the stored hash and returns true if they match. The password is checked in code, never in the SQL.

The full safe flow

  1. User submits username and password.
  2. Fetch the row by username using a prepared statement.
  3. If a row exists, password_verify the password against its hash.
  4. On match, start the session; otherwise show a generic "invalid login" message.

Use the same generic error for wrong username and wrong password, so attackers cannot tell which was wrong.

Common mistakes

  • Comparing passwords in SQL. Fetch by username, verify in code.
  • Storing plain passwords. Always hash with password_hash.
  • Building SQL with user input. Use prepared statements.
  • Revealing which field was wrong. Use one generic error message.

Summary

  • Never store plain passwords — store a bcrypt hash (password_hash).
  • Never put user input directly in SQL — use prepared statements.
  • Fetch the user by username, not by password.
  • Verify the password in code with password_verify.
  • Show one generic error for any failed login.

Note: this is a sensitive security topic; always follow current best practices. Next, guard queries in safe UPDATE and DELETE, or filter rows in SELECT and WHERE.

Frequently Asked Questions

How do I write a secure login query in MySQL?
Never put user input directly in the SQL and never compare plain passwords. Instead, fetch the user by username using a prepared statement, then verify the entered password against the stored hash with a function like PHP's password_verify.
Why should I not check the password inside the SQL query?
Putting the password in the WHERE clause means it must be stored in plain text or a simple hash, and building the query with user input invites SQL injection. The safe pattern fetches the row by username, then verifies the password in code against a strong hash.
How do I store passwords safely in MySQL?
Never store plain passwords. Store a strong one-way hash created with a function like PHP's password_hash (bcrypt). At login you re-hash and compare with password_verify. Even if the database leaks, the real passwords are not exposed.
What is a prepared statement and why use it for login?
A prepared statement sends the SQL and the values separately, so user input can never change the query structure. This stops SQL injection, where a crafted username could otherwise bypass login or expose data.
How does password_verify work?
password_verify takes the password the user typed and the stored hash, and returns true if they match. It handles the hashing internally, so you never compare raw passwords or write your own hash-checking logic.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicUpdate Multiple Rows Your progress: 143 / 151 (95%) · Lesson 144 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free