Delete Query
How to Delete records from DataBase.

To Delete records from MySQL DataBase.

Syntax:-
DELETE FROM table_name WHERE column_name=value

Example:-
DELETE FROM tbl_users WHERE id=5

This mysql query delete user record where id is 5

Note:- where clause is necessary for delete particular records otherwise all records will be delete from database.

Delete Data through PHP & Mysql

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

$con = mysql_connect($server,$username,$password);
if (!$con)
die('Mysql not connect ');

$database=mysql_select_db($database, $con);
if (!$database)
die('DataBase not connect ');

$result=mysql_query("delete from info where id=5");

if ($result) {
echo "Record deleted successfully";
}

mysql_close($con);
?>

Delete Data through PHP & Mysqli (Procedural)

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

$conn = new mysqli($server, $username, $password, $database);

if (!$conn) {
die("Mysql not Connected");
}

$query = "delete from info where id=1";
$result=mysqli_query($conn, $sql)

if ($result) {
echo "Record deleted successfully";
}

mysqli_close($conn);
?>

Delete Data through PHP & Mysqli (Object oriented)

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

$conn = new mysqli($server, $username, $password, $database);

if ($conn->connect_error) {
die("Not Connected");
}

$query = "delete from info where id=1";
$result=$conn->query($query);

if ($result) {
echo "Record deleted successfully";
}

$conn->close();
?>

Delete Data through PHP & Mysql (PDO)

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

try {
$conn = new PDO("mysql:host=$server;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "delete from info where id=2";

$conn->exec($query);
echo "Record deleted successfully";
}
catch(PDOException $e)
{
echo "Record not deleted";
}

$conn = null;
?>


Previous
Next