Hi everyone,Today I will show you how to connect MySQL database from PHP and do CRUD operation. CRUD mean Create, Read, Update, Delete. It very basic for work with database. If you want store, show, change, or remove data, you need do CRUD. Now let’s start step by step.
If you want to learn more, please check our: [PHP Tutorial in Khmer Playlist].
For example db.php:
To test the connect successfully or not you can run the "db.php".
See example below:
For example:
For example:
For example:
I hope you enjoy learning PHP with me and don't forget to share this post to your friends. Keep learning and sharing.
Caring is sharing!!! - happy coding
Free Khmer Ebook Download (PDF): Database | Microsoft Access | Python Programming

If you want to learn more, please check our: [PHP Tutorial in Khmer Playlist].
1. Connect PHP with MySQL Database
The easy way to connect PHP with MySQL database is using mysqli. Make sure you watch the video for more detail explaination in Khmer.For example db.php:
<?php
$servername = "localhost"; // Database server
$username = "root"; // Database username
$password = ""; // Database password
$database = "mydatabase"; // Database name
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
To test the connect successfully or not you can run the "db.php".
How to connect with MySQL with PHP and verify connection - PHP khmer tutorial
2. Create Data (Insert Record)
To Insert data we need to use SQL statement "INSERT INTO". Just make sure your connect is ok.See example below:
<?php
$sql = "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
How to Insert data to MySQL database with form in PHP - PHP khmer tutorial
3. Read Data (Get Records)
To get data from database you can use "SELECT" statement by choosing any fields with or without condition base on your need.For example:
<?php
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "
";
}
} else {
echo "0 results";
}
?>
How to get data from database with php and Display in table - PHP khmer tutorial
4. Update Data (Change Record)
To update data in database we use "UPDATE" statement with specific condition and usually with id.For example:
<?php
$sql = "UPDATE users SET name='Sok sitha' WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
?>
How to UPDATE data with Form to MySQL Database - PHP khmer tutorial
5. Delete Data (Remove Record)
To delete data from database usually we use "DELETE" statement but in real world application we rarely delete data, we "UPDATE" it instead.For example:
<?php
$sql = "UPDATE users SET is_active =0 WHERE id=1";
//or
//$sql = "DELETE FROM users WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error delete record: " . $conn->error;
}
?>
How to DELETE data from MySQL with PHP - PHP khmer tutorial
6. Close Connection
After all work done, we close connection.
<?php
$conn->close();
?>
I hope you enjoy learning PHP with me and don't forget to share this post to your friends. Keep learning and sharing.
Caring is sharing!!! - happy coding
Free Khmer Ebook Download (PDF): Database | Microsoft Access | Python Programming