Php mysqli batch replace database table prefix instances

Source: Internet
Author: User

In php, sometimes we need to replace the table prefix in the database, but it does not suffer from changing the prefix for tables one by one. Below I write a php program that replaces the database table prefix in batches by mysqli, I hope some methods will help you.

The Code is as follows: Copy code


<? Php
Header ('HTTP-equiv = "Content-Type" content = "text/html; charset = UTF-8 "');
$ DB_host = "localhost"; // Database Host
$ DB_user = "root"; // Database User
$ DB_psw = "root3306"; // Database Password
$ DB_datebase = "gk_yue39_com"; // Database Name
$ DB_charset = "utf8"; // database Character Set
$ Dbprefix = "yue392_com _";
$ New_dbprefix = "yue39_com _";
$ Db = new mysqli ($ DB_host, $ DB_user, $ DB_psw); // instantiate an object

// Check the connection
If (mysqli_connect_errno ()){
Printf ("Connect failed: % sn", mysqli_connect_error ());
Exit ();
}

$ Db-> select_db ($ DB_datebase); // Select Operation Database

$ Db-> set_charset ($ DB_charset); // sets the database character set.

// Execute a query
$ SQL = 'show tables ';
$ Result = $ db-> query ($ SQL );

Echo $ result-> num_rows. 'row result'. $ result-> field_count. 'column content <br/> ';

// $ Result-> data_seek ('5'); // obtain the result starting from 5th entries in the result set

Echo '<table border = "1" cellspacing = "0" cellpadding = "0" align = "center" width = "90%"> ';

// Cyclic output field name
// $ Result-> field_seek (2); // obtain the result from 2nd entries in the field set
While (true = ($ field = $ result-> fetch_field ())){
Echo '<th> '. $ result-> current_field. '_'. $ field-> name. '('. $ field-> length. ') </th> ';
}

// Output query results cyclically
While (true = ($ row = $ result-> fetch_assoc ())){
Echo '<tr> ';
Foreach ($ row as $ col ){
$ SQL = "rename table". $ col. "'to'". str_replace ($ dbprefix, $ new_dbprefix, $ col )."'";
If ($ db-> query ($ SQL )){
Echo '<td align = "center"> '. $ SQL. '</td> <font color = "blue"> success </font> </td> ';
} Else {
Echo '<td align = "center"> '. $ SQL. '</td> <font color = "red"> failed </font> </td> ';
}
}
Echo '</tr> ';
}

Echo '</table> ';
$ Result-> free (); // release the result set
$ Db-> close (); // close the connection
?>

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.