0% found this document useful (0 votes)
8 views

Mysql Practical Questions

Uploaded by

rj801503
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Mysql Practical Questions

Uploaded by

rj801503
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Creating a Database and Table

 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 id (INT, primary key, auto-increment)

o name (VARCHAR(50))

o email (VARCHAR(50), unique)

o created_at (TIMESTAMP, default as current timestamp)

 Task: Write a PHP script that connects to MySQL, creates the user_management database,
and then creates the users table within it.

2. Inserting Data into the Database

 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.

3. Displaying Data from the Database

 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.

4. Updating Data in the Database

 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.

5. Deleting Data from the Database

 Scenario: A user with id = 2 requests to delete their account.

 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

 Scenario: You want to search for a specific user by their 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.

7. Handling User Authentication

 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.

8. File Upload and Save Path to Database

 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.

9. Pagination of User Records

 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.

10. Joining Multiple Tables

 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 post_id (INT, primary key, auto-increment)

o user_id (INT, foreign key referencing users(id))

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.

11. Form Validation with PHP and Database Interaction

 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.

You might also like