• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> PHP >> How to Empty All Tables in a MySQL Database with PHP

How to Empty All Tables in a MySQL Database with PHP

January 26, 2010 by Lorensius Londa 6 Comments

This is a simple PHP script to empty all tables in a MySQL database. This script first will list all tables in a database using ‘SHOW TABLES‘ statement then it will empty  each table using  ‘TRUNCATE TABLE‘ statement.

$con = mysql_connect('dbhost','dbuser','dbpassword');

if (!$con) die("Can't connect to database!");

mysql_select_db('dbname');

$res = mysql_query('SHOW TABLES', $con);

while ($row = mysql_fetch_array($res, MYSQL_NUM))
{
  $res2 = mysql_query("TRUNCATE TABLE `$row[0]`", $con);
}

About TRUNCATE Statement

For an InnoDB table before version 5.0.3, InnoDB processes TRUNCATE TABLE by deleting rows one by one. As of MySQL 5.0.3, row by row deletion is used only if there are any foreign key constraints that reference the table. If there are no foreign key constraints, InnoDB performs fast truncation by dropping the original table and creating an empty one with the same definition, which is much faster than deleting rows one by one.

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Set Time Zone in PHP and MySQL
  2. How to Enable MySQL InnoDB Storage Engine on XAMPP
  3. Prototype: Using Ajax.Updater to Update Browser’s DOM Element
  4. How To Define Global Absolute Path in PHP

Filed Under: Information Technology, PHP, Programming Tagged With: database, empty table mysql, Mysql, PHP, show tables, truncate table

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. ihsandulu says

    October 13, 2012 at 7:02 am

    Tnks Very Much!

    Reply
  2. aisjit says

    November 17, 2012 at 6:24 pm

    secrets of yoga? natural wieght loss?? lol wtf???

    Reply
  3. Steven says

    September 27, 2014 at 1:13 am

    Thank you, envatoproducts.com

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

  • @tricahyono_bowo @infobandung @infobdg Wah kejauhan om 355 days ago
  • Wilujeng enjing sadayana..Mohon info tempat powder coating dan sandblasting yg recommended di Bandung dunk @infobandung @infobdg359 days ago

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in