PHP MySQLi delete data
PHP Tutorial » PHP MySQLi delete data
If you need to delete a record from any MySQL table, you can use the command DELETE FROM.
Syntax:
Let's look at the "Users" table: We will have six columns as: "id", "first_name", "last_name", "user_name", "email" and "reg_date":
How do you delete data from this table in CSS?
ID | First Name | Last Name | UserName | |
---|---|---|---|---|
1 | Clark | Kent | clarkkent | clarkkent@mail.com |
2 | John | Carter | johncarter | johncarter@mail.com |
3 | Peter | Parker | peterparker | peterparker@mail.com |
In the next examples we delete the record with id=2 in the "Users" table:
Delete record in DataBase MySQLi (object oriented)
In the next examples we delete the record with id=2 in the "Users" table:
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = new mysqli($server_name, $user_name,
$password, $db_name);// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM Users WHERE id=2";
if ($link->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $link->error;
}
$link->close();
?>
Related subjects:
How to create database phpmyadmin?
Insert multiple rows
PHP select / view data
Conect to MySQLi
PHP MySQLi create table
Tags: php mysql delete record with confirmation, how to delete data from database in php using button, how to delete data from database in php using button pdo, how to delete a row in mysql using php, how to delete all data from table in mysql using php, mysql delete row, how to delete data from database in php using button, mysql delete join
Delete record in DataBase MySQLi (Procedural)
In the next examples we delete the record with id=2 in the "Users" table using MySQLi (Procedural)
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = mysqli_connect($server_name, $user_name,
$password, $db_name);
// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM Users WHERE id=2";
if (mysqli_query($link, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($link);
}
mysqli_close($link);
?>
Delete record in DataBase MySQLi using (PDO) extension
The following example delet a row in table named "Users" in database my_DB using PDO
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
try {
$link = new PDO("mysql:host=$server_name;dbname=$db_name",
$user_name, $password);
// set the PDO error mode to exception
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to delete a record
$sql = "DELETE FROM Users WHERE id=2";
// use exec() because no results are returned
$link->exec($sql);
echo "Record deleted successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$link = null;
?>
ID | First Name | Last Name | UserName | |
---|---|---|---|---|
1 | Clark | Kent | clarkkent | clarkkent@mail.com |
2 | Peter | Parker | peterparker | peterparker@mail.com |
how to delete data from database in php using button, how to delete data from database in php using button pdo, how to delete a row in mysql using php, delete mysql database, how to delete all data from table in mysql using php, delete data from table mysql, mysql delete row
PHP MySQLi delete data - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

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

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.