Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP Delete Data From MySQL
 
Delete Data From a MySQL Table Using MySQLi and PDO
 
The DELETE statement is used to delete records from a table:
  1. DELETE FROM table_name
  2. WHERE some_column = some_value
Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!
 
Let's look at the "MyGuests" table:
id firstname lastname email reg_date
1 John Doe john@example.com 2014-10-22 14:26:15
2 Mary Moe mary@example.com 2014-10-23 10:22:30
3 Julie Dooley julie@example.com 2014-10-26 10:48:23
 
The following examples delete the record with id=3 in the "MyGuests" table:
 
Example (MySQLi Object-oriented)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10.     die("Connection failed: " . $conn->connect_error);
  11. // sql to delete a record
  12. $sql = "DELETE FROM MyGuests WHERE id=3";
  13. if ($conn->query($sql) === TRUE) {
  14.     echo "Record deleted successfully";
  15. } else {
  16.     echo "Error deleting record: " . $conn->error;
  17. }
  18. $conn->close();
  19. ?>
 
Example (MySQLi Procedural)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = mysqli_connect($servername, $username, $password, $dbname);
  8. // Check connection
  9. if (!$conn) {
  10.     die("Connection failed: " . mysqli_connect_error());
  11. }
  12. // sql to delete a record
  13. $sql = "DELETE FROM MyGuests WHERE id=3";
  14. if (mysqli_query($conn, $sql)) {
  15.     echo "Record deleted successfully";
  16. } else {
  17.     echo "Error deleting record: " . mysqli_error($conn);
  18. }
  19. mysqli_close($conn);
  20. ?>
 
Example (PDO)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDBPDO";
  6. try {
  7.     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  8.     // set the PDO error mode to exception
  9.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.     // sql to delete a record
  11.     $sql = "DELETE FROM MyGuests WHERE id=3";
  12.     // use exec() because no results are returned
  13.     $conn->exec($sql);
  14.     echo "Record deleted successfully";
  15.     }
  16. catch(PDOException $e)
  17.     {
  18.     echo $sql . "<br>" . $e->getMessage();
  19.     }
  20. $conn = null;
  21. ?>
 
After the record is deleted, the table will look like this:
id firstname lastname email reg_date
1 John Doe john@example.com 2014-10-22 14:26:15
2 Mary Moe mary@example.com 2014-10-23 10:22:30
 

 



2016-12-10
588 views




 
     
 
Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy

Catalog.299.ru

The site is a media provider
Яндекс.Метрика
 
English USA
Australia Русский