DELETE FROM table_name
WHERE column_name
= some_value
MySQL query using the DELETE
statement and WHERE
clause, after that we will execute this query through passing it to the PHP mysqli_query()
function to student_details_2 the tables records. The following student_detail_2 table inside the demo database :
ID | Name | College | Email ID | Mobile |
---|---|---|---|---|
1 | Chanti | SV College | chanti@freetimelearn.com | 9966463846 |
2 | Ramana Reddy | Vidhyanikethan | ramana@freetimelearn.com | 9963666068 |
3 | Roja Reddy | Vidhyalaya | roja@freetimelearn.com | 99010023210 |
4 | V V R R | Narayana | vvrr@freetimelearn.com | 9182401686 |
<!DOCTYPE html>
<html>
<head>
<title>PHP MySQL DELETE Query</title>
</head>
<body>
<?php
// Create connection
$ftl_connect = mysqli_connect("localhost", "root", "", "ftl_db");
// Check connection
if (!$ftl_connect) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM student_details_2 WHERE stu_id=4";
if (mysqli_query($ftl_connect, $sql)) {
echo "Record has successfully deleted..!";
} else {
echo "Error deleting record : " . mysqli_error($ftl_connect);
}
//Close Connection
mysqli_close($ftl_connect);
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>PHP MySQL DELETE Query</title>
</head>
<body>
<?php
// Create connection
$ftl_connect = mysqli_connect("localhost", "root", "", "ftl_db");
// Check connection
if ($ftl_connect->connect_error) {
die("Connection failed: " . $ftl_connect->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM student_details_2 WHERE stu_id=4";
if ($ftl_connect->query($sql) === TRUE) {
echo "Record has successfully deleted..!";
} else {
echo "Error deleting record : " . $ftl_connect->error;
}
//Close Connection
$ftl_connect->close();
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>PHP MySQL DELETE Query</title>
</head>
<body>
<?php
try {
$ftl_connect = new PDO("mysql:host=localhost;dbname=ftl_db", "root", "");
// set the PDO error mode to exception
$ftl_connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to delete a record
$sql = "DELETE FROM student_details_2 WHERE stu_id=4";
// use exec() because no results are returned
$ftl_connect->exec($sql);
echo "Record has successfully deleted..!";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
//Close Connection
$ftl_connect = null;
?>
</body>
</html>
ID | Name | College | Email ID | Mobile |
---|---|---|---|---|
1 | Chanti | SV College | chanti@freetimelearn.com | 9966463846 |
2 | Ramana Reddy | Vidhyanikethan | ramana@freetimelearn.com | 9963666068 |
3 | Roja Reddy | Vidhyalaya | roja@freetimelearn.com | 99010023210 |