php+mysqli实现批量替换数据库表前缀的方法_php技巧
本文实例讲述了php+mysqli实现批量替换数据库表前缀的方法。分享给大家供大家参考。具体分析如下:
在php中有时我们要替换数据库中表前缀但是又不苦于一个个表去修改前缀,这里我自己写了一个mysqli批量替换数据库表前缀的php程序,感兴趣的朋友可以参考一下,代码如下:
<?php header ( 'http-equiv="Content-Type" content="text/html; charset=utf-8"' ); $DB_host = "localhost"; //数据库主机 $DB_user = "root"; //数据库用户 $DB_psw = "root3306"; //数据库密码 $DB_datebase = "gk_yue39_com"; //数据库名 $DB_charset = "utf8"; //数据库字符集 $dbprefix="yue392_com_"; $new_dbprefix="yue39_com_"; $db = new mysqli ( $DB_host, $DB_user, $DB_psw ); //实例化对象 //检查连接 if (mysqli_connect_errno ()) { printf ( "Connect failed: %sn", mysqli_connect_error () ); exit (); } $db->select_db ( $DB_datebase ); //选择操作数据库 $db->set_charset ( $DB_charset ); //设置数据库字符集 //执行一个查询 $sql = 'show tables'; $result = $db->query ( $sql ); echo $result->num_rows . ' 行结果 ' . $result->field_count . ' 列内容<br/>'; //$result->data_seek('5');//从结果集中第5条开始取结果 echo '<table border="1" cellspacing="0" cellpadding="0" align="center" width="90%">'; //循环输出字段名 //$result->field_seek(2);//从字段集中第2条开始取结果 while ( true == ($field = $result->fetch_field ()) ) { echo '<th>' . $result->current_field . '_' . $field->name . '(' . $field->length . ')</th>'; } //循环输出查询结果 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><td><font color="blue"> success</font></td>'; }else{ echo '<td align="center">' . $sql. '</td><td><font color="red"> failed</font></td>'; } } echo '</tr>'; } echo '</table>'; $result->free ();//释放结果集 $db->close (); //关闭连接 ?>
第二种方法:如何批量修改MYSQL的数据库表前缀名称
批量修改表名的操作方法,以下操作请用navicat操作即可,快捷方便:
SELECT CONCAT( ‘ALTER TABLE ‘, TABLE_NAME, ‘RENAME TO ‘, TABLE_NAME,‘;' )
FROM information_schema.TABLES
WHERE TABLE_NAME LIKE ‘uc_%';
执行后得到如下的结果:
ALTER TABLE uc_aaa RENAME TO uc_aaa;
ALTER TABLE uc_bbb RENAME TO uc_bbb;
保留如下的数据:
ALTER TABLE uc_aaa RENAME TO uc_aaa;
ALTER TABLE uc_bbb RENAME TO uc_bbb;
然后选择要修改的数据库,执行上面得到的SQL语句就可以修改掉数据库表前缀了。
附:1、批量删除指定前缀的表
SELECT CONCAT( ‘drop table ‘, TABLE_NAME, ‘;' )
FROM information_schema.TABLES
WHERE TABLE_NAME LIKE ‘uc_%';
2、“dbtable_name”改成“db_table_name”
SELECT CONCAT( ‘ALTER TABLE ‘, TABLE_NAME, ‘RENAME TO db_', SUBSTRING(TABLE_NAME,3),‘;' )
FROM information_schema.TABLES
WHERE TABLE_NAME LIKE ‘db%';
第三种方法:
今天更新一个mysql数据库表前缀的另一个方法,这个方法使用也非常简单,就是用第三方工具——帝国备份王进行修改。操作方法如下:
1、登录帝国备份后台,选择“备份数据”—— “批量替换表名”,然后执行操作就可以完成批量替换了,也非常简单。如下图:
如何批量修改mysql的表前缀名称
以上方法都可修改网站前缀,不过大多数的开源程序修改表缀后,再需要修改网站程序的数据库配置文件,才会让网站网站的运行。
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
