AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQL Delete Data

PHP-MySQLi Tutorial » PHP MySQL Delete Data

How to Delete Data From MySQL Table Using MySQLi and PDO

DELETE statement is used to delete records from a table:

Syntax:

DELETE FROM table_name
WHERE some_column = some_value
)
Note: The WHERE clause specifies which record should be deleted. If you omit the WHERE clause, then all records will be deleted!

 

Let's look at the "Students" table:

id first_name last_name email date
1 Johny Mare jm@example.com 2021-01-22 15:26:15
2 Maya Moere mayam@example.com 2021-01-23 16:22:30
3 Julia Doolyte julid@example.com 2021-01-26 17:48:23

The following examples delete the record with id=2 in the "Students" table:

 

PHP MySQL Delete Data ( MySQLi Object-oriented ) - example

Open a Connection to MySQL

How to delete data in MySQL database using PHP from database "Students" table:

Example: (MySQLi Object-Oriented)

<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "my_DB";

// Create connection
$con = new mysqli($server_name, $user_name, $password, $db_name);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}

// sql to delete a record
$sql = "DELETE FROM Students WHERE id=2";

if ($con->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $con->error;
}

$con->close();
?>

 

PHP MySQL delete row from table( PHP MySQLi Procedural ) Example

How to delete records using PHP MySQLi Procedural

Example: ( MySQLi Procedural )

<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "my_DB";

// Create connection
$con = mysqli_connect($server_name, $user_name, $password, $db_name);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM Students WHERE id=2";

if (mysqli_query($con, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($con);
}

mysqli_close($con);
?>

 

PDO delete row from Database - example

Example (PDO) delete row from Database - is a little bit different:

Example: (PDO)

<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "my_DB_PDO";

try {
$con = new PDO("mysql:host=$server_name;dbname=$db_name", $user_name, $password);
// set the PDO error mode to exception
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// sql to delete a record
$sql = "DELETE FROM Students WHERE id=2";

// use exec() because no results are returned
$con->exec($sql);
echo "Record deleted successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}

$con = null;
?>

Note: In the PDO example above we have also specified a database (my_DB_PDO).

The result:

id first_name last_name email date
1 Johny Mare jm@example.com 2021-01-22 15:26:15
3 Julia Doolyte julid@example.com 2021-01-26 17:48:23

 



How to delete data in mysql database using php, PHP delete row from Database, PHP MySQL Delete Data from table, php mysql delete row button, php mysql delete row from table, deleting data, php mysql delete duplicate records, php mysql delete row after time, php mysql delete row button tutorial, php mysql delete records older than 24 hours, row count
PHP MySQL Delete Data - php mysqli

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.