Ask Question


Delete Data From MySQL Using PHP

To delete data from MySQL the DELETE statement is used. We can delete data from specific column or all column of a table.

To delete selected column data from database the SQL query is

DELETE FROM table_name WHERE some_column=some_value;

To delete all the column data from a table the SQL query is

DELETE FROM table_name;


DELETE * FROM table_name;

In the below example we delete the employee data from MySQL database.

In this example we used 2 file for retrieve data


die('Could not Connect My Sql:' .mysql_error());


include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM employee");
<!DOCTYPE html>
<link rel="stylesheet" href="style.css">
<title>Delete employee data</title>
<td>Employee Id</td>
<td>First Name</td>
<td>Last Name</td>
<td>Email id</td>
while($row = mysqli_fetch_array($result)) {
<tr class="<?php if(isset($classname)) echo $classname;?>">
<td><?php echo $row["userid"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["city_name"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><a href="delete-process.php?userid=<?php echo $row["userid"]; ?>">Delete</a></td>


include_once 'database.php';
mysqli_query($conn,"DELETE FROM employee WHERE userid='" . $_GET["userid"] . "'");
echo "Data delete sucessfully";

delete-process.php Alternative Code (PDO)

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "crud";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
// sql to delete a record
$sql = "DELETE FROM employee WHERE userid='" . $_GET["userid"] . "'";
// use exec() because no results are returned
echo "Record deleted successfully";
catch(PDOException $e)
echo $sql . "
" . $e->getMessage();
$conn = null;

Buy Template