PHP+mysqli implements batch replacement of database table prefixes, mysqli prefix_PHP tutorial

WBOY
Release: 2016-07-13 10:10:29
Original
981 people have browsed it

php+mysqli implements batch replacement of database table prefixes, mysqli prefix

The example in this article describes how php+mysqli implements batch replacement of database table prefixes. Share it with everyone for your reference. The specific analysis is as follows:

In PHP, sometimes we need to replace the table prefixes in the database, but we don’t have to worry about modifying the prefixes one by one. Here I wrote a MySQL PHP program that replaces the database table prefixes in batches. Interested friends can refer to the code. As follows:

Copy code The code is as follows:
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 object

//Check 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 ); //Set database character set

//Execute a query
$sql = 'show tables';
$result = $db->query ( $sql );

echo $result->num_rows . ' Row results ' . $result->field_count . ' Column content
';

//$result->data_seek('5');//Get results starting from the 5th item in the result set

echo '';

//Loop output field names
//$result->field_seek(2);//Get results starting from the 2nd item in the field set
while ( true == ($field = $result->fetch_field ()) ) {
echo '';
}

//Loop to output query results
while ( 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 be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/934933.htmlTechArticlephp+mysqli method to realize batch replacement of database table prefix, mysqli prefix This article describes the example of php+mysqli to realize batch replacement Database table prefix method. Share it with everyone for your reference. ...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template