如有疑問,加群交流:646104701
源碼運行結果截圖:

PHP批量替換MySql數據庫內容 UTF-8 1.0版
<?php
//聲明
//1、本源碼開發意圖:作者在使用一些CMS建站的時候發現很多CMS把網址寫入到數據庫了,如果換網址,那么就需要更新數據庫中的老網址,如果一個一個修改,很不方便,所以開發此源碼,供大家學習或使用;
//2、源碼開發者:楊波;
//3、源碼開發者聯系QQ:773003231;
//4、源碼開發者博客:http://www.cnblogs.com/phpyangbo/;
//5、源碼開放性:任何人都可以隨意更改或使用本源碼,本源碼為開源並免費使用,不存在版權。
//替換數據庫內容類
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>
