PHP MySQL Delete Data

PHP Tutorial

 PHP MySQL Delete Data Tutorial

In PHP MySQL Delete Data is used to remove records from a MySQL table. In PHP, you can execute DELETE queries using mysqli or PDO.

Warning: Without a WHERE clause, DELETE will remove all rows in the table.


 Using mysqli (Procedural)


 

  •  Deletes the record where id = 3.

 Using mysqli Object-Oriented


 

  •  Object-oriented style uses $conn->query().

Using PDO


 

  •  Using prepared statements ensures security against SQL injection.

Delete All Records


 

  •  Always be careful! Use WHERE to avoid removing all records unintentionally.

Check How Many Rows Were Deleting


 

This helps confirm how many records were removing.


Difference Between DELETE and TRUNCATE

DELETE removes records row by row and supports WHERE condition
TRUNCATE removes all records instantly and resets auto increment
TRUNCATE cannot be rolled back

Use DELETE when conditions are needing.


 Key Points

  1. DELETE removes one or more records from a table.

  2. Always include a WHERE clause unless you intend to delete all records.

  3. Use prepared statements for dynamic or user-supplied data.

  4. mysqli_query() or $conn->query() executes the DELETE query.

  5. For PDO, use prepare() and execute() for safety.

You may also like...