Home  >  Article  >  Backend Development  >  mysql batch replace database tables

mysql batch replace database tables

WBOY
WBOYOriginal
2016-08-08 09:26:531173browse
<pre name="code" class="php"><?php
// +----------------------------------------------------------------------
// | 批量替换数据库表
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: Wing 
// +----------------------------------------------------------------------

mysql_connect("localhost","root","123");
mysql_select_db("ts");
mysql_query("set names utf8");
$old_prefix=&#39;mr_&#39;;//数据库的前缀
$new_prefix=&#39;jmyk_&#39;;//数据库的前缀修改为

$res = mysql_query(&#39;show tables&#39;);


//遍历数据表
while ($rows = mysql_fetch_object($res)) {
    $data[]= $rows->Tables_in_jiameiyake;
}


// print_r($data);

//表前缀
foreach($data as $k => $v)
{
        $preg = preg_match("/^($old_prefix{1})([a-zA-Z0-9_-]+)/i", $v, $v1);

        if($preg)
        {
                $tab_name[$k] = $v1[2];
        }
}

// print_r($tab_name);

if($preg)
{
	//批量替换表前缀
    foreach ($tab_name as $key => $value) {
        $sql = "rename table ".$old_prefix.$value." to " . $new_prefix . $value ;
        echo $sql."<br />";
        $a = mysql_query($sql);
        if($a){
            print  "数据表:" . $old_prefix.$value . "已经修改为:".$new_prefix.$value;
        }
    }

}


                
                
                

The above introduces mysql batch replacement of database tables, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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