Php batch change database table prefix

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. We often encounter the problem of replacing or adding the database table prefix. Export the database, change it in batch in notepad, and then export it again, this is also a method, but it is not convenient. Through the following method, you can easily solve the problem. The Code is as follows .? Php $ dat

Welcome to the Linux community forum and interact with 2 million technical staff> we often encounter the problem of replacing or adding the database table prefix when entering the Forum. Export the database and change it in batches in notepad, this is also a method, but it is not convenient. The following method makes it easy to solve the problem. The Code is as follows. ? Php $ dat

Welcome to the Linux community forum and interact with 2 million technicians>

We often encounter the problem of replacing or adding the database table prefix. This is also a method, but it is not convenient enough. You can export the database, change it in batches in notepad, and then export it again.

The following method makes it easy to solve the problem. 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.

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

Change to $ 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.