Delete Record From a MySQL Database using PHP
Table Name tbl_student_record
ID | Name | Class | Subject |
1 | Mohan | 2nd | Math |
2 | Pooja | 2nd | Art |
3 | Suraj Kumar | 3nd | Scince |
Object-Oriented
<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here
// In this line Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// In this line Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// In this line delete a record and WHERE clause specifies which record you want to be delete.
$sql = "DELETE FROM tbl_student_record WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record has been deleted successfully";
} else {
echo "Record has not been deleted successfully " . $conn->error;
}
$conn->close();
?>
Procedural
<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here
// In this line Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// In this line Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// In this line delete a record and WHERE clause specifies which record you want to be delete.
$sql = "DELETE FROM tbl_student_record WHERE id=2";
if (mysqli_query($conn, $sql) or die(mysqli_error($conn)) ) {
echo "Record has been deleted successfully";
} else {
echo "Record has not been deleted successfully ";
}
mysqli_close($conn);
?>
Output
ID | Name | Class | Subject |
1 | Mohan | 2nd | Math |
3 | Suraj Kumar | 3nd | Scince |
Quick ask your Question
- Connect mysql database using php Language
- Insert record into MySQL Database Using PHP Language
- Get Record from MySQL Database in PHP
- Update Record from MySQL Database in PHP
- Delete Record From a MySQL Database using PHP
- Insert Multiple Records into MySQL with a Single Query
- Create MySQL Database Using PHP
- Create Table in MySQL Database Using PHP