First of all a short introduction to the MySQL RDBMS

MySQL is the world's most used relational database management system that runs as a server providing multi-user access to a number of databases. It is named after developer Michael Widenius' daughter, My. The SQL phrase stands for Structured Query Language.

The MySQL development project has made its source code available under the terms of the GNU General Public License, as well as under a variety of proprietary agreements. MySQL was owned and sponsored by a single for-profit firm, the Swedish company MySQL AB, now owned by Oracle Corporation. Free-software-open source projects that require a full-featured database management system often use MySQL.

For commercial use, several paid editions are available, and offer additional functionality. Applications which use MySQL databases include: TYPO3, Joomla, WordPress, phpBB, Drupal and other software built on the LAMP software stack. MySQL is also used in many high-profile, large-scale World Wide Web products, including Wikipedia, Google (though not for searches), Facebook, and Twitter.

And now here you are how to do the job:

There are two ways to delete all the data from a MySQL table, really you can choose a lot of others ways, but you can find these ways very expensive:

 – The TRUNCATE way

 

simply type the following SQL command in a MySQL shell

TRUNCATE TABLE tablename;

The above command will delete all data in the table very quickly, this command drops the table and then recreate it. This is the fastest method.

 

 – The DELETE way

 

simply type the following SQL command in a MySQL shell

DELETE FROM tablename;

The above command will delete all rows from the table, this command is just a little slower than the previous.

 


ONE OR THE OTHER

If you have an auto increment value in the table you want to delete, you must pay attention to the different behaviour of the two commands. The TRUNCATE commands resets the auto increment value to 1 while the DELETE command doesn't change its value.

Using the PHP

The implementation of these two commands in php is very simple.

The first thing you have to do is the connection to the database:

    /** MySQL database name */

    define('DB_NAME', 'yourdbname');

    /** MySQL database username */

    define('DB_USER', 'yourdbuser');

    /** MySQL database password */

    define('DB_PASSWORD', 'yourdbpassword');

    /** MySQL hostname */

    define('DB_HOST', 'yourdbhost');

 

        // Initialization

    $conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

    mysql_select_db(DB_NAME, $conn);

    // Error checking

    if(!$conn) {

        die('Could not connect ' . mysql_error());

    }

Then you can issue the commands:

    $table = "theTable";





    // The truncate way

    $sql = "TRUNCATE TABLE '$theTable'";



    // The delete way

    $sql = "DELETE FROM '$theTable'";



    

    $result = mysql_query($sql,$conn);

    if(!$result)

    {

        die("ERR");

    }


That's all,

Gg1