DELETING DATA FROM MYSQL DATABASE

Summary: in this tutorial, you will learn how to lớn delete data from MySquốc lộ database table by using PHPhường PDO.

Bạn đang xem: Deleting data from mysql database

We will use the tasks table in the sample database for the demonstration. Before going forward with this tutorial, you should follow the PHPhường MySQL create tabletutorial to lớn create the tasks table and insert sample data for practicing.

See the following tasks table.

*

To delete data in a table, you use the following steps:

Exexinh đẹp the DELETE statement by calling the exec() method of the PDO object or the execute() method of the PDOStatement object.

PHP MySQL Delete data examples

PHPhường. MySQL:delete a single row example

To delete a single row in a table, you use the DELETE statement with a WHERE clause that specifies which row to lớn delete.

The following script deletes a row with id 2 in the tasks table.


/** * PHP MySquốc lộ Delete Data Demo */class DeleteDataDemo const DB_HOST = "localhost"; const DB_NAME = "classicmodels"; const DB_USER = "root"; const DB_PASSWORD = ""; /** * PDO instance *
var PDO */ private $pvị = null; /** * Open a database connection khổng lồ MySQL */ public function __construct() // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s", self::DB_HOST, self::DB_NAME); try $this->pvị = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); catch (PDOException $e) die($e->getMessage()); /** * Delete a task based on a specified task id *
param int $id *
return bool true on success or false on failure */ public function delete($id) $sql = "DELETE FROM tasks WHERE task_id = :task_id"; $q = $this->pdo->prepare($sql); return $q->execute(<":task_id" => $id>); /** * cthua the database connection */ public function __destruct() $this->pbởi vì = null; $obj = new DeleteDataDemo();// delete id 2$obj->delete(2);
Code language: HTML, XML (xml)How it works.

Xem thêm: What Is Php? The End Is Near For Zend Server Basic Php

In the __construct() method of the DeleteDataDemo class, we connect to lớn the MySQL database by initiating an instance of the PDO class, và in the __destruct() method, we cthua thảm the database connection.

The delete() method accepts the idas the argument. First, we Call the prepare() method of the PDO object khổng lồ prepare the DELETE statement for execution, & then we pass an array that contains values corresponding khổng lồ the named placeholders in the DELETE statement lớn the execute() method of the PDOStatement object.

To delete the task with id 2, we create an instance of the DeleteDataDemo class & Call the delete() method.

PHPhường MySQL:delete all rows in a table examples

There are two ways to delete all rows in a table:

Issue a TRUNCATE TABLEstatement.

The following deleteAll() method deletes all rows in the tasks table using the DELETE statement:


/** * Delete all rows in the tasks table */public function deleteAll()$sql = "DELETE FROM tasks";return $this->pdo->exec($sql);
Code language: HTML, XML (xml)The following truncateTable() method removes all rows in the tasks table as well as resets its autoincrement column’s value:


/** * Truncate the tasks table */public function truncateTable() $sql = "TRUNCATE TABLE tasks";return $this->pdo->exec($sql);
Code language: PHP (php)You can download the source code of this tutorial via the following link:

Download PHP. MySquốc lộ Delete Source Code

In this tutorial, we have sầu shown you how to lớn delete data from MySQL table using PHPhường PDO.


Was this tutorial helpful?
Previous PHP MySQL: Update Data
Next PHPhường MySQL: call MySquốc lộ Stored Procedures
MySQL Quiông chồng Start
PHPhường. MySquốc lộ Tutorial
MySquốc lộ Programming Interfaces
Others
Recent MySQL Tutorials
About MySQL Tutorial Website

obatambeienwasirherbal.com is a website dedicated to MySQL database. We regularly publish useful MySQL tutorials khổng lồ help web developers and database administrators learn MySquốc lộ faster and more effectively.

All MySQL tutorials are practical và easy-to-follow, with Squốc lộ script và screenshots available. More About Us


Site Links