Php + mysqli to replace database table prefixes in batches. mysqli prefix _ PHP Tutorial

Source: Internet
Author: User
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' ';} // 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' ';} Else {Echo' ';}}Echo' ';} Echo'
'. $ Result-> current_field.' _ '. $ field-> name.' ('. $ field-> length .')
'. $ SQL .' Success'. $ SQL .' Failed
';
$ 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 ....

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.