Php batch Change database table prefix implementation method _ php instance

Source: Internet
Author: User
We often encounter the problem of replacing or adding the database table prefix. it is also a method to export data through the database, change it in batch in notepad, and then export the data, but it is not convenient to use the following method to easily solve the problem. The code is as follows, which can be used as a top point.

The code is as follows:


$ Database = "databaseName"; // database name
$ User = "root"; // database username
$ Pwd = "pwd"; // database password
$ Replace = 'pre _ '; // The prefix after replacement
$ Seach = 'pre1 _ '; // The prefix to be replaced
$ Db = mysql_connect ("localhost", "$ user", "$ pwd") or die ("database connection failed:". mysql_error (); // connect to the database

$ Tables = mysql_list_tables ("$ database ");
While ($ name = mysql_fetch_array ($ tables )){

$ Table = str_replace ($ seach, $ replace, $ name ['0']);

Mysql_query ("rename table $ name [0] to $ table ");
}

?>


If you want to add a prefix, you only need to change it a little bit.

The code is as follows:


$ Table = str_replace ($ seach, $ replace, $ name ['0 ']);
$ Table = $ replace. $ name ['0'];


You can.

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.