Delete data in MySQL
Similarly as you supplement records into tables, you can erase records from a table utilizing the SQL DELETEstatement. It is regularly utilized in conjugation with the WHERE provision to erase just those records that matches explicit criteria or condition.
The essential grammar of the DELETE proclamation can be given with:
Erase FROM table_name WHERE column_name=some_value
We should make a SQL question utilizing the DELETE proclamation and WHERE provision, after that we will execute this inquiry through passing it to the PHP mysqli_query() capacity to erase the tables records. Think about the accompanying people table inside the demo database:
The PHP code in the accompanying model will erase the records of those people from the people table whose first_name is equivalent to John.
Delete Query - Procedural Method :
{` {'$mysqli = new mysqli("localhost", "root", "", "Mydb");'} { 'if( $mysqli === false ){'} {'die("ERROR: Could not connect. " . $mysqli->connect_error);'} {' }'} {' $sql = "DELETE FROM Data WHERE ID=201";'} {'if($mysqli->query($sql) === true){'} {'echo "Record was deleted successfully.";'} { ' } else{'} {' echo "ERROR: Could not able to execute $sql. "'} {' . $mysqli->error;'} {' }'} { '$mysqli->close();'} `}
Table After Updation
Delete Query - PDO Method :
{` {'try{'} {'$pdo = new PDO("mysql:host=localhost;'} dbname=Mydb", "root", ""); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); {'} catch(PDOException $e){'} die("ERROR: Could not connect. " . $e->getMessage()); {'}'} {'try{'} $sql = "DELETE FROM Data WHERE ID=201"; $pdo->exec($sql); echo "Record was deleted successfully."; {'} catch(PDOException $e){'} die("ERROR: Could not able to execute $sql. " . $e->getMessage()); {'}'} unset($pdo); `}
Table After Updation
MYSQL Tutorials
- Create MySQL Database
- Create Table in MySQL
- Insert data to MySQL
- Mysql Queries
- Update data in MYSQL
- Delete data in MySQL
- Default Database
- Primary Key
- Foreign Keys
- Views
- Export MySQL Database
- Import data into Mysql
- Create Website with MySQL
MYSQL Sample Assignments
- COIT20247 Database Design and Development
- Relational schema and MySQL database
- Mysql Project
- ITICT107A Introduction to Databases
- MySql Assignment Question
MYSQL Sample Solutions
- Solution - COIT20247 Database Design
- Solution - Relational schema and MySQL
- Solution - Mysql Project
Testimonials
Very affordable projects!! And that to submit before deadlines. Thanks for helping me in my database project and raising my grades. I have been able to secure good marks in my internal assessment only because of you. Read More