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:
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 '' . $result->current_field . '_' . $field->name . '(' . $field->length . ') | ';
}
//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 '' . $sql. ' | success | ';
}else{
echo '' . $sql. ' | failed | ';
}
}
echo '
';
}
echo '
';
$result->free ();//Release the result set
$db->close (); //Close the connection
?>
I hope this article will be helpful to everyone’s PHP programming design.