How To Connect Login Page With Database In Php

PHP Programming

Connecting a login page with a database is crucial when it comes to building a secure and functional website. In this article, I will guide you through the process of connecting a login page with a database using PHP. I will provide step-by-step instructions and add my personal touches and commentary along the way.

Setting Up the Database

The first step in connecting a login page with a database is to create the database itself. I prefer using MySQL as it is widely supported and easy to work with. You can use a tool like phpMyAdmin or a command-line interface to create a new database.

Once the database is created, you will need to create a table to store user information. For simplicity, let’s create a table named “users” with the following columns:

username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL

Creating the Login Page

Now that the database is set up, let’s move on to creating the login page. Start by creating a new PHP file and add the necessary HTML and CSS code to create the login form. Don’t forget to add the form action attribute and set it to the PHP file that will handle the login logic.

Inside the PHP file that handles the login logic, we need to establish a connection with the database. You can use the mysqli extension or PDO to interact with the database. Here is an example of connecting to the database using mysqli:

$servername = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_database";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

Make sure to replace “your_username”, “your_password”, and “your_database” with the actual credentials for your database. If the connection fails, an error message will be displayed.

Handling the Login Logic

Once the connection is established, we can proceed to handle the login logic. Start by retrieving the username and password entered by the user from the login form. You can use the $_POST superglobal to access the form data.

Next, write a SQL query to check if the entered username and password match any records in the “users” table. Here is an example:

$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = $conn->query($query);

if ($result->num_rows == 1) {
// Login successful
// Redirect to the home page or any other protected area
} else {
// Login failed
// Display an error message or redirect back to the login page

This query selects all rows from the “users” table where the username and password match the user’s input. If a matching record is found, the login is successful, and you can redirect the user to the home page or any other protected area. Otherwise, you can display an error message or redirect them back to the login page.


Connecting a login page with a database in PHP is an essential part of building a secure website. By following the steps outlined in this article, you can create a functional login page that securely interacts with a database. Remember to always handle user input with care and implement additional security measures, such as password encryption. Happy coding!