php批量更改數(shù)據(jù)庫表前綴實現(xiàn)方法
更新時間:2013年10月26日 09:05:43 作者:
我們經(jīng)常會遇到替換或添加數(shù)據(jù)庫表前綴的問題,通過數(shù)據(jù)庫導(dǎo)出,在記事本上批量更改,然后再導(dǎo)出,這也是一種方法,但是不夠方便
通過下面這個方法,輕松搞定,代碼如下,有用到的頂起。
復(fù)制代碼 代碼如下:
<?php
$database = "databaseName"; //數(shù)據(jù)庫名稱
$user = "root"; //數(shù)據(jù)庫用戶名
$pwd = "pwd"; //數(shù)據(jù)庫密碼
$replace ='pre_'; //替換后的前綴
$seach = 'pre1_'; //要替換的前綴
$db=mysql_connect("localhost","$user","$pwd") or die("連接數(shù)據(jù)庫失?。?.mysql_error()); //連接數(shù)據(jù)庫
$tables = mysql_list_tables("$database");
while($name = mysql_fetch_array($tables)) {
$table = str_replace($seach,$replace,$name['0']);
mysql_query("rename table $name[0] to $table");
}
?>
如果是添加前綴只需要變化一點點
復(fù)制代碼 代碼如下:
$table = str_replace($seach,$replace,$name['0']);換成
$table = $replace.$name['0'];
就可以了。
相關(guān)文章
PHPMYADMIN導(dǎo)入數(shù)據(jù)最大為2M的解決方法
PHPMYADMIN還原數(shù)據(jù)庫的時候上傳的最大限制:2,048 KB,數(shù)據(jù)庫稍微大一些就無法處理,要么使用其他的備份還原工具如帝國備份王,要么就分卷導(dǎo)出,那么有沒有辦法還使用PHPMYADMIN來處理呢2012-04-04