Login Page in Python with MySQL: Sample Code

Hello all welcome back to this blog we are creating login using python and MySQL

import pymysql
import sys

# Connect to the database
db = pymysql.connect("localhost", "username", "password", "database")

# Create a cursor object
cursor = db.cursor()

# Prompt for login credentials
username = input("Username: ")
password = input("Password: ")

# Construct the query
query = "SELECT * FROM users WHERE username = %s AND password = %s"

# Execute the query
cursor.execute(query, (username, password))

# Fetch the result
result = cursor.fetchone()

# If the result is empty, then the credentials were invalid
if result == None:
    sys.exit("Invalid credentials")

# Otherwise, the credentials were valid
print("Login successful!")

# Close the connection

Leave a Reply