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

PHP MySQLi delete data


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

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:

DELETE FROM table_name WHERE column_name = value
Note: WHERE clause specifies which record should be deleted. If you omit WHERE clause, all records will be deleted!

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 Email
1 Clark Kent clarkkent [email protected]
2 John Carter johncarter [email protected]
3 Peter Parker peterparker [email protected]

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:

Example (this is HTML - PHP editor, change text on this window)

<?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)

Example (this is HTML - PHP editor, change text on this window)

<?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

Example (this is HTML - PHP editor, change text on this window)

<?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 Email
1 Clark Kent clarkkent [email protected]
2 Peter Parker peterparker [email protected]

 



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

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.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
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.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...