Home > php教程 > php手册 > body text

php mysqli批量替换数据库表前缀实例

WBOY
Release: 2016-05-25 16:38:01
Original
1245 people have browsed it

在php中有时我们要替换数据库中表前缀但是又不苦于一个个表去修改前缀吧,下面我自己写了一个mysqli批量替换数据库表前缀的php程序,希望些方法对你有帮助,代码如下:

<?php
header(&#39;http-equiv="Content-Type" content="text/html; charset=utf-8"&#39;);
$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 = &#39;show tables&#39;;
$result = $db->query($sql);
echo $result->num_rows . &#39; 行结果  &#39; . $result->field_count . &#39; 列内容<br/>&#39;;
//$result->data_seek(&#39;5&#39;);//从结果集中第5条开始取结果
echo &#39;<table border="1" cellspacing="0" cellpadding="0" align="center" width="90%">&#39;;
//循环输出字段名
//$result->field_seek(2);//从字段集中第2条开始取结果
while (true == ($field = $result->fetch_field())) {
    echo &#39;<th>&#39; . $result->current_field . &#39;_&#39; . $field->name . &#39;(&#39; . $field->length . &#39;)</th>&#39;;
}
//循环输出查询结果
while (true == ($row = $result->fetch_assoc())) {
    echo &#39;<tr>&#39;;
    foreach ($row as $col) {
        $sql = "rename table `" . $col . "` to `" . str_replace($dbprefix, $new_dbprefix, $col) . "`";
        if ($db->query($sql)) {
            echo &#39;<td align="center">&#39; . $sql . &#39;</td><td><font color="blue"> success</font></td>&#39;;
        } else {
            echo &#39;<td align="center">&#39; . $sql . &#39;</td><td><font color="red"> failed</font></td>&#39;; //开源代码phprm.com
        }
    }
    echo &#39;</tr>&#39;;
}
echo &#39;</table>&#39;;
$result->free(); //释放结果集
$db->close(); //关闭连接
Copy after login


本文地址:

转载随意,但请附上文章地址:-)

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template