Code for batch deleting data tables with the same prefix in Mysql in php

Source: Internet
Author: User

Method 1:
Copy codeThe Code is as follows:
<? Php
Mysql_connect ('','','');
Mysql_select_db ('');
$ Rs = mysql_query ('show tables ');
While ($ arr = mysql_fetch_array ($ rs )){
$ TF = strpos ($ arr [0], 'class _');
If ($ TF = 0 ){
$ FT = mysql_query ("drop table $ arr [0]");
If ($ FT ){
Echo "$ arr [0] deleted successfully! <Br> ";
}
}
}
?>


Method 2:
I re-installed the site today. After one afternoon, I finally found a way to delete database tables in batches...
This example uses the prefix xx _. You can change it to the table prefix you want to delete.
Copy codeThe Code is as follows:
<? Php
Function deldata ($ dbname, $ tableflag ){
$ Db_host = 'localhost ';
$ Db_port = '000000 ';
$ Db_user = 'user ';
$ Db_pass = 'Password ';
$ Connect = mysql_connect ($ db_host, $ db_user, $ db_pass );
Mysql_select_db ($ dbname );
$ Result = mysql_query ("show table status from $ dbname", $ connect );
$ Data = mysql_fetch_array ($ result );
While ($ data = mysql_fetch_array ($ result )){
$ Table = mysubstr ($ data [Name], "_");
If ($ table = $ tableflag ){
// Use for testing
/* Echo $ data [Name];
Echo"
";
Echo $ table;
Echo"
";*/
Mysql_query ("drop table $ data [Name]");
}
}
Return true;
}
/* Truncates all the character functions before a specific character
* $ Str is the string to be truncated.
* $ Flag specific characters such as "_"
*/
Function mysubstr ($ str, $ flag ){
$ Pos = strpos ($ str, $ flag );
Return substr ($ str, 0, $ pos );
}
?>

The change is:
1. Starting

<? Php
Function deldata ($ dbname, $ tableflag ){
$ Db_host = 'localhost ';
$ Db_port = '000000 ';
$ Db_user = 'user ';
$ Db_pass = 'Password ';
Change your database address, account, and password.
2. End

Change your database name and the table prefix you want to delete
You can copy the above Code and save it as. php, and upload it to the Space Directory to open it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.