ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

SQL Delete Statement

sqltuts

If you want to delete a record from any MySQL table, then you can use SQL DELETE Statement.

1.)command at mysql> prompt

2.) any script like PHP.

 
DELETE FROM table_name [WHERE Clause]
 

The WHERE clause is used when a user  wants to delete selected rows in a table of a given database.

Deleting Data from Command Prompt:

SQL DELETE Statment with WHERE clause is used in order to delete selected data into MySQL table tutorials_table.

Example:

  1. root@host# mysql -u root -p password;
  2. Enter password:*******
  3. mysql> use Database TUTORIALS;
  4. Database changed
  5. mysql> DELETE FROM Database tutorials_table WHERE tutorial_id=15;
  6. Query OK, 1 row affected (0.23 sec)
  7.  
  8. mysql>

 

Deleting Data Using PHP Script:

Example:

 

  1. <?php
  2. $dbhost = 'localhost';
  3. $dbusername = 'root';
  4. $dbpassword = 'rootpassword';
  5. $conn = mysql_connect($dbhost, $dbusername, $dbpassword);
  6. if(! $con )
  7. {
  8.   die('Could not connect: ' . mysql_error());
  9. }
  10. $sql = 'DELETE FROM Datbase tutorials_table
  11.        WHERE tutorial_id=15';
  12.  
  13. mysql_select_db('DatabaseTUTORIALS');
  14. $retval = mysql_query( $sql, $con );
  15. if(! $result )
  16. {
  17.   die('Could not be able to delete data: ' . mysql_error());
  18. }
  19. echo "Deleted  the desired data successfully\n";
  20. ?>
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!