PHP MySQL Basics
PHP MySQL Basics
Here are some basic PHP MySQL commands that help you connect, insert, retrieve, update, and
delete data in a MySQL database using PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb"; // Replace with your database name
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
📥 2. Insert Data
php
<?php
$sql = "INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')";
if (mysqli_query($conn, $sql)) {
echo "Record inserted successfully";
} else {
echo "Error: " . mysqli_error($conn);
}
?>
<?php
$sql = "SELECT id, name, email FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/22
}
?>
✏️ 4. Update Data
php
<?php
$sql = "UPDATE users SET email='[email protected]' WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
?>
❌ 5. Delete Data
php
<?php
$sql = "DELETE FROM users WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
?>
🔚 6. Close Connection
php
<?php
mysqli_close($conn);
?>
Would you like me to generate a complete sample CRUD project using these commands?
Here are basic MySQL commands used in the terminal (command-line interface), especially helpful
when working directly with the MySQL server:
🔐 1. Login to MySQL
bash
mysql -u username -p
Example:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/22
bash
mysql -u root -p
SHOW DATABASES;
Example:
sql
USE dbname;
Example:
sql
USE school;
SHOW TABLES;
🧱 6. Create a Table
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/22
sql
DESCRIBE tablename;
or
sql
DESC tablename;
EXIT;
or
sql
QUIT;
Let me know if you want examples using a real table like students, or how to back up/restore a database
from the terminal.
Sure, Vamthu! Let's understand CRUD operations in PHP in the simplest language with examples.
🧠 What is CRUD?
CRUD stands for:
Operation Meaning
C Create (Insert data into database)
R Read (Display data from database)
U Update (Change data in database)
<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<?php
$name = "John";
$email = "[email protected]";
<?php
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
<?php
$sql = "UPDATE users SET email='[email protected]' WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error: " . mysqli_error($conn);
}
?>
<?php
$sql = "DELETE FROM users WHERE id=1";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error: " . mysqli_error($conn);
}
?>
🔚 Close Connection
php
<?php
mysqli_close($conn);
?>
Would you like a full CRUD webpage example using forms (HTML + PHP)?
Sure, Vamthu! Let's explain the Read operation again in the easiest way with example and steps.
👇 Step-by-Step Explanation:
✅ 1. Connect to Database
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/22
php
<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
if (mysqli_num_rows($result) > 0) {
// loop through data
}
<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/22
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// show each row one by one
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"] . "<br>";
echo "Name: " . $row["name"] . "<br>";
echo "Email: " . $row["email"] . "<br><hr>";
}
} else {
echo "No data found.";
}
mysqli_close($conn);
?>
📌 Output Example:
makefile
ID: 1
Name: John
Email: [email protected]
----------------------------
ID: 2
Name: Alice
Email: [email protected]
Uploaded image
explain in detail as an...
<?php
$conn = new mysqli("localhost", "root", "", "testdb");
$stmt->close();
$conn->close();
?>
🧠 bind_param Types:
Type Meaning
i Integer
d Double
s String
b Blob (binary)
DELIMITER //
CREATE PROCEDURE GetUsers()
BEGIN
SELECT * FROM users;
END //
DELIMITER ;
<?php
$conn = new mysqli("localhost", "root", "", "testdb");
$conn->close();
?>
📚 Summary:
Feature Prepared Statement Stored Procedure
Sure, Vamthu! Here's a simple and clear explanation of Data Types and Operators in PHP, with
examples.
🔹 2. Compound Types
Type Example
array $arr = array(1, 2, 3);
🔹 3. Special Types
Type Example
NULL $x = NULL;
resource $file = fopen("test.txt", "r");
🧮 OPERATORS in PHP
PHP operators are used to perform operations on variables and values.
🔹 1. Arithmetic Operators
Used to do math.
* Multiplication 4 * 2
/ Division 10 / 2
% Modulus (Remainder) 5 % 2
🔹 2. Assignment Operators
Used to assign values.
-= $a -= 5; $a = $a - 5
*= $a *= 2; $a = $a * 2
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/22
🔹 3. Comparison Operators
Used in conditions (like if).
🔹 4. Logical Operators
Used to combine conditions.
🔹 5. String Operator
Used to join strings.
🔹 6. Increment/Decrement
Operator Meaning Example
++ Increment by 1 $a++
-- Decrement by 1 $a--
🔹 7. Ternary Operator
Short if-else condition:
php
✅ Summary Table:
Data Type Example
int 5, 100
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/22
Data Type Example
float 10.5, 3.14
string "Hello"
resource fopen()
Sure Vamthu! Here's a simple explanation of Selection (Conditional) and Loop Statements in PHP,
with easy examples.
🔸 if Statement
php
$age = 18;
🔸 if...else Statement
php
$marks = 45;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/22
🔸 if...elseif...else
php
$score = 80;
🔸 switch Statement
php
$day = "Tuesday";
switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Tuesday":
echo "Second day";
break;
default:
echo "Other day";
}
🔸 while Loop
php
$i = 1;
🔸 do...while Loop
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/22
php
$i = 1;
do {
echo "Count: $i<br>";
$i++;
} while ($i <= 3);
🔸 for Loop
php
🔸 foreach Loop
php
🧠 Summary
Type Statement Used For
Would you like a small mini program using both selection and loops together?
Absolutely Vamthu! Here's a simple explanation of arrays in PHP, including how to create, access
elements, and the types of arrays — with clear examples.
📦 What is an Array?
An array is a variable that can store multiple values in a single variable.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/22
✅ How to Create an Array in PHP?
php
php
php
2. Associative Array
php
$person = [
"name" => "Vamthu",
"age" => 20,
"city" => "Chennai"
];
3. Multidimensional Array
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/22
php
$students = [
["John", 18],
["Vamthu", 20],
["Sara", 19]
];
php
php
📌 Summary Table
Type Key Type Example Access
Indexed Array Number $arr[0]
Associative Array Named keys $arr["name"]
Would you like a small quiz or program to test your array skills?
Great question, Vamthu! Let’s understand when to use echo and when to use print in PHP, with easy
explanation and examples.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/22
✅ 1. What is echo in PHP?
echo is used to display output.
🔸 Example:
php
🔸 Example:
php
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/22
✅ Best Practice
Use echo most of the time because it’s faster and more flexible.
Use print only when you need a return value (e.g., in expressions or conditional output).
Would you like a small program to try both echo and print together?
Sure, Vamthu! Let's understand form processing in PHP in the simplest way possible, step by step —
with example.
✅ Step-by-Step Example
📌 Step 1: Create an HTML Form
html
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/22
📌 Step 2: Create PHP File to Process Form
php
🧠 Key Points
Concept Description
if (empty($name) || empty($email)) {
echo "Please fill all fields!";
} else {
echo "Welcome, $name!";
}
✅ Summary
Use HTML to create the form.
Use PHP to receive and process data.
Form validation?
Saving form data to a database?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 22/22