<pre name="code" class="php"><?php // +---------------------------------------------------------------------- // | 批量替换数据库表 // +---------------------------------------------------------------------- // | // +---------------------------------------------------------------------- // | Author: Wing // +---------------------------------------------------------------------- mysql_connect("localhost","root","123"); mysql_select_db("ts"); mysql_query("set names utf8"); $old_prefix='mr_';//数据库的前缀 $new_prefix='jmyk_';//数据库的前缀修改为 $res = mysql_query('show tables'); //遍历数据表 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; } } }
以上就介紹了mysql 批量替換資料庫表,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。