Mysql Practical Questions
Mysql Practical Questions
Scenario: You are tasked with creating a system to store user data. Write a PHP script that
creates a MySQL database called user_management and a table users with the following
fields:
o name (VARCHAR(50))
Task: Write a PHP script that connects to MySQL, creates the user_management database,
and then creates the users table within it.
Scenario: You need to register a new user into your users table. The user’s name is "John
Doe" and email is "[email protected]".
Task: Write a PHP script that connects to the user_management database and inserts the
above user information into the users table. Use MySQLi functions.
Scenario: You want to display all the users' information in the users table in a tabular format
on a webpage.
Task: Write a PHP script that connects to the user_management database, retrieves all users'
data, and displays it in a table format (HTML). Ensure the table includes columns for ID,
Name, Email, and Creation Date.
Scenario: You realize that you need to update the email of user with id = 1 to
"[email protected]".
Task: Write a PHP script that updates the email address of the user with id = 1. Display a
confirmation message if the update is successful.
Task: Write a PHP script that deletes the user from the users table where the id = 2. Display a
message confirming that the record has been deleted.
6. Searching for a User by Email
Task: Write a PHP script that allows the user to input an email address and then search the
users table for that email. If found, display the user’s information, otherwise, display a "User
not found" message.
Scenario: You are building a simple login system. Assume the users table has a password
column (hashed passwords).
Task: Write a PHP script that allows users to log in by entering their email and password.
Upon submission, check if the credentials match a record in the users table, and if so, display
a "Login successful" message. If not, display an "Invalid credentials" message.
Scenario: You are building a profile page where users can upload a profile picture. The file
path of the uploaded image should be stored in the users table under the column profile_pic.
Task: Write a PHP script that allows users to upload a profile picture. Once the file is
uploaded, save the file path in the profile_pic column of the users table for that user. Display
a message confirming that the picture has been uploaded and path saved.
Scenario: You have many users in the database, and you want to paginate the display of user
records, showing 10 users per page.
Task: Write a PHP script that retrieves and displays 10 users per page from the users table.
Provide next and previous buttons for pagination.
Scenario: You are asked to add a new table posts to the user_management database, where
users can create posts. The table posts has the following fields:
o title (VARCHAR(100))
o content (TEXT)
o created_at (TIMESTAMP)
Task: Write a PHP script that displays all posts, along with the name and email of the user
who created the post, using a JOIN query.
Scenario: You want to implement a registration form with validation. Before inserting a new
user into the database, you need to ensure that the email address is not already registered.
Task: Write a PHP script with a form that validates the user input. Check if the email already
exists in the users table. If it does, display an error message. If it doesn't, insert the new user.