Php + mysqli implements the method of replacing the database table prefix in batches by using the mysqli prefix. Php + mysqli to replace the database table prefix in batches. This document describes how to replace the database table prefix in batches using php + mysqli. Share it with you for your reference. Php + mysqli to replace the database table prefix in batches.
This example describes how to replace the database table prefix in batches using php + mysqli. Share it with you for your reference. The specific analysis is as follows:
In php, sometimes we need to replace the table prefix in the database, but it does not suffer from changing the prefix in tables. here I write a php program that replaces the database table prefix in batches by mysqli, for more information, see the following code:
The code is as follows:
<? 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
';
// $ Result-> data_seek ('5'); // obtain the result starting from 5th entries in the result set
Echo'
'; // Cyclic output field name// $ Result-> field_seek (2); // obtain the result from 2nd entries in the field setWhile (true = ($ field = $ result-> fetch_field ())){Echo'
'. $ Result-> current_field.' _ '. $ field-> name.' ('. $ field-> length .') | ';} // Output query results cyclicallyWhile (true = ($ row = $ result-> fetch_assoc ())){Echo'
';Foreach ($ row as $ col ){$ SQL = "rename table". $ col. "'to'". str_replace ($ dbprefix, $ new_dbprefix, $ col )."'";If ($ db-> query ($ SQL )){Echo'
'. $ SQL .' |
Success | ';} Else {Echo'
'. $ SQL .' |
Failed | ';}}Echo'
';} Echo'
';
$ Result-> free (); // release the result set
$ Db-> close (); // close the connection
?>
I hope this article will help you with php programming.
Examples in this article describes how to replace database table prefixes in batches using php + mysqli. Share it with you for your reference ....