### Extended Summary of MySQL & PHP Topics
1. MySQL Basics
Creating a Database
CREATE DATABASE my_database;
Selecting a Database
USE my_database;
Creating a Table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
age INT
);
Inserting Data
INSERT INTO users (name, email, age)
VALUES ('Alice', '[email protected]', 25);
Retrieving Data
SELECT * FROM users;
SELECT name, age FROM users WHERE age > 20;
Updating Data
UPDATE users SET age = 26 WHERE name = 'Alice';
Deleting Data
DELETE FROM users WHERE name = 'Alice';
2. Primary & Foreign Keys
Primary Key
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50)
);
Foreign Key
CREATE TABLE courses (
course_id INT PRIMARY KEY,
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id) ON DELETE
CASCADE
);
3. PHP & MySQL Connection
Database Connection File (db_connect.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "my_database";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
4. PHP Variables & Arrays
Declaring Variables
$name = "John";
$age = 25;
$is_student = true;
Arrays in PHP
$fruits = array("Apple", "Banana", "Orange");
echo $fruits[0]; // Apple
5. PHP Control Statements
If-Else Statement
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
For Loop
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br>";
}
6. Inserting Data Using PHP Form
HTML Form (index.php)
<form action="insert.php" method="post">
<input type="text" name="name" required>
<input type="email" name="email" required>
<input type="text" name="mobile" required>
<input type="submit" name="submit" value="Submit">
</form>
PHP Insert Script (insert.php)
<?php
include 'db_connect.php';
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$sql = "INSERT INTO users (name, email, mobile) VALUES ('$name',
'$email', '$mobile')";
mysqli_query($conn, $sql);
}
?>
7. Fetching and Displaying Data
PHP Fetch Script (fetch.php)
<?php
include 'db_connect.php';
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . " - " . $row['email'] . "<br>";
}
?>
8. Updating Data Using PHP
Update Script (update.php)
<?php
include 'db_connect.php';
$id = $_GET['id'];
$sql = "UPDATE users SET name='New Name' WHERE id=$id";
mysqli_query($conn, $sql);
?>
9. Deleting Data Using PHP
Delete Script (delete.php)
<?php
include 'db_connect.php';
$id = $_GET['id'];
$sql = "DELETE FROM users WHERE id=$id";
mysqli_query($conn, $sql);
?>
10. Questions & Answers
True or False
1. A PRIMARY KEY allows duplicate values in a column. (False)
2. mysqli_connect() is used to connect PHP to MySQL. (True)
3. The SELECT * FROM users; query retrieves all columns from the users
table. (True)
4. DELETE FROM users; removes the table structure. (False)
5. A foreign key helps link two tables together. (True)
Multiple Choice Questions
1. What SQL command is used to create a database?
o a) ADD DATABASE
o b) MAKE DATABASE
o c) CREATE DATABASE
o d) INSERT DATABASE
Answer: c)
2. What does mysqli_fetch_assoc() do?
o a) Inserts data into a table
o b) Retrieves one row from a MySQL result as an associative
array
o c) Deletes a record
o d) Updates a table Answer: b)
3. Which method is used to send form data securely in PHP?
o a) GET
o b) POST
o c) FETCH
o d) PUT Answer: b)
4. How do you set a primary key in SQL?
o a) PRIMARY
o b) SET PRIMARY
o c) PRIMARY KEY
o d) UNIQUE KEY Answer: c)
5. What function is used to close a database connection in PHP?
o a) close()
o b) mysqli_close()
o c) exit()
o d) disconnect() Answer: b)