So far, I have discussed about connection establishment, create table, insert record, read records (select statement), update record. In this article we are going to learn about delete a record.
Following is the table which I am going to delete a record. This table name is emp_info
emp_id | emp_name | emp_age |
---|---|---|
1 | Raj | 22 |
2 | Rahul | 24 |
3 | Vivek | 26 |
4 | Ajay | 28 |
5 | Suraj | 29 |
In the above table, I want to delete the record whose id is 4
Assuming, you have followed previous tutorials, I am focusing straight to the code.
Delete Record
$conn=mysqli_connect(“localhost”,”root”,””,”emp_db”);
$sql=”delete from emp_info where id=4″;
if(mysqli_query($conn,$sql)){
echo “record deleted successfully”;
}else{
echo “Unable delete record”:
}
Output: Record deleted
So after deleting the record, the table is like below.
emp_id | emp_name | emp_age |
---|---|---|
1 | Raj | 22 |
2 | Rahul | 24 |
3 | Vivek | 26 |
5 | Suraj | 29 |