源碼運行結果截圖:php
PHP批量替換MySql數據庫內容 UTF-8 1.0版html
<?php //聲明 //一、本源碼開發意圖:做者在使用一些CMS建站的時候發現不少CMS把網址寫入到數據庫了,若是換網址,那麼就須要更新數據庫中的老網址,若是一個一個修改,很不方便,因此開發此源碼,供你們學習或使用; //二、源碼開發者:楊波; //三、源碼開發者聯繫QQ:773003231; //四、源碼開發者博客:http://www.cnblogs.com/phpyangbo/; //五、源碼開放性:任何人均可以隨意更改或使用本源碼,本源碼爲開源並無償使用,不存在版權。 //替換數據庫內容類 class replace{ public $dbAddress; //數據庫地址 public $dbUser; //數據庫用戶名 public $dbPwd; //數據庫密碼 public $dbName; //數據庫名稱 public $dbPort; //數據庫端口 public $keywords; //須要替換的關鍵字 public $result_keywords; //替換成什麼 //數據庫鏈接 public function dbConnect($dbAddress,$dbUser,$dbPwd,$dbName,$dbPort=''){ if(empty($dbPort)){ $dbPort = '3306'; } $this->dbAddress = $dbAddress; $this->dbUser = $dbUser; $this->dbPwd = $dbPwd; $this->dbName = $dbName; $this->dbPort = $dbPort; //數據庫鏈接 $dbCon = mysql_connect($dbAddress.':'.$dbPort,$dbUser,$dbPwd); //數據庫鏈接驗證 if($dbCon){ //數據庫鏈接成功 //指定數據庫 $assign = mysql_select_db($dbName,$dbCon); if($assign){ mysql_query("set names 'utf8'"); //設置要使用的字符集 return array('return'=>true,'ps'=>'數據庫鏈接成功'); } else{ return array('return'=>false,'ps'=>'指定數據庫失敗'); } } else{ //數據庫鏈接失敗 return array('return'=>false,'ps'=>'數據庫鏈接失敗:'.mysql_error()); } } //查詢全部表 public function queryTable(){ $rs = mysql_query("SHOW TABLES FROM ".$this->dbName); $tables = array(); while ($row = mysql_fetch_row($rs)) { $tables[] = $row[0]; } mysql_free_result($rs); return $tables; } //查詢全部帶關鍵字的數據並替換 /* table 數據庫中的全部表名數組 keywords 查詢的關鍵字 result 要替換成什麼 */ public function queryReplace($table,$keywords='',$result_keywords=''){ $this->keywords = $keywords; $this->result_keywords = $result_keywords; $arr = array(); //裝載返回信息 $index = 1; //自增值 //循環全部表 foreach($table as $key=>$v){ $result = mysql_query('select * from '.$v); for ($i=0;$i<mysql_num_fields($result);$i++){ $fieldName = mysql_field_name($result,$i); //到這裏,數據庫名稱是 $this->dbName 表名是 $v 字段名是 $fieldName $fieldResult = mysql_query('select '.$fieldName.' from '.$v); while($fieldRow = mysql_fetch_array($fieldResult)){ //判斷該字段中的數據內容是否存在將要替換的關鍵字 $fieldValue = $fieldRow[$fieldName]; if(strpos($fieldValue,$keywords) !== false){ //若是存在就繼續執行替換 $replaceBack = str_replace($keywords,$result_keywords,$fieldValue); //更換數據 if(mysql_query('update '.$v.' set '.$fieldName.'="'.$replaceBack.'" where '.$fieldName.'="'.$fieldValue.'"')){ $arr[$index]["dbName"] = $this->dbName; $arr[$index]["tableName"] = $v; $arr[$index]["fieldName"] = $fieldName; $index++; } } } } } return $arr; } } //程序邏輯 $replace = new replace(); //實例化類 $steps = $_GET["steps"]; //執行步驟 //dbSet數據庫信息設置 //detection檢測 if(empty($steps)){ $steps = 'dbSet'; } if($steps=='detection'){ $dbAddress = $_POST["dbAddress"]; $dbUser = $_POST["dbUser"]; $dbPwd = $_POST["dbPwd"]; $dbName = $_POST["dbName"]; $dbPort = $_POST["dbPort"]; $keywords = $_POST["keywords"]; $result_keywords = $_POST["result_keywords"]; if(empty($dbAddress) || empty($dbUser) || empty($dbPwd) || empty($dbName) || empty($dbPort) || empty($keywords)){ die("帶星號的值必須填寫"); } $db = $replace->dbConnect($dbAddress,$dbUser,$dbPwd,$dbName,$dbPort); $queryTable = $replace->queryTable(); } //如下爲HTML ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>批量替換MySql數據庫內容 UTF-8 1.0版 - 做者:楊波 聯繫QQ:773003231</title> <style> *{margin:0;padding:0;font-size:12px;} .box{ width: 300px; padding: 20px; border: 1px solid #eee; margin: 0 auto; margin-top: 150px; background-color: #fcfcfc; } h1{ font-size: 16px; line-height: 40px; font-weight: bold; color: #333; } h2{ line-height: 25px; font-weight: normal; color: #999; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #eee; margin-bottom: 15px; } p{ min-height: 30px; } p input{ border: 1px solid #ccc; padding-top: 3px; padding-right: 5px; padding-bottom: 3px; padding-left: 5px; } a{ color: #03F; } span{ line-height: 25px; color: #F00; } </style> </head> <body> <div class="box"> <h1>批量替換MySql數據庫內容 UTF-8 1.0版</h1> <h2>做者:楊波 聯繫QQ:773003231 本程序由於編碼是UTF-8因此只支持此類型編碼的數據庫替換,若是您是其它類型的數據庫請修改本源碼</h2> <?php if($steps=='dbSet'){?> <form id="form1" name="form1" method="post" action="?steps=detection"> <p>數據庫地址:<input type="text" name="dbAddress" value="localhost" /> * <p>數據庫用戶:<input type="text" name="dbUser" /> * <p>數據庫密碼:<input type="text" name="dbPwd" /> * <p>數據庫名稱:<input type="text" name="dbName" /> * <p>數據庫端口:<input type="text" name="dbPort" value="3306" /> * <p>須要替換的關鍵字:<input type="text" name="keywords" /> * <p>替換成什麼關鍵字:<input type="text" name="result_keywords" /> <p><span>注意:此操做不可撤銷,進入下一步以前,請您先備份將要執行替換操做的數據庫,若是您進入下一步,形成的任何後果,做者不承擔任何責任,此源碼僅用於學習交流,請勿用於任何商業使用</san> <p><input type="submit" name="button" id="button" value=" 開始替換 " style="margin-left:90px;margin-top:30px;"/> </form> <?php }else if($steps=='detection'){?> <p>數據庫狀態:<?=$db['ps']?> <p>正在替換... <p>替換完成</p> <p>共替換:<?=count($replace->queryReplace($queryTable,$keywords,$result_keywords))?>次 <p><a href="?">返回上一步</a></p> <?php }?> </div> </body> </html>