PHP Bulk replaces data content in MySQL database (replace MySQL database content source code)

Source: Internet
Author: User

PHP bulk replaces MySQL database content UTF-8 version 1.0

<?php//declaration//1, the Source code development intention: the author in the use of some CMS to build a station when the site found many CMS to write to the database, if you change the URL, then you need to update the old Web site in the database, if a change, very inconvenient, so the development of this source code for everyone to learn or use; 2, source Developer: Yang Bo;//3, source developer Contact QQ:773003231;//4, source Developer Blog: HTTP://WWW.CNBLOGS.COM/PHPYANGBO/;//5, open Source: Anyone can change or use the source code, Origin code is open source and free to use, there is no copyright. Replace database content class Replace{public $dbAddress;//database address public $dbUser;//database user name public $dbPwd;//Database password public $dbName;// Database name public $dbPort;//database port public $keywords;//the keyword to be replaced public $result _keywords;//replaced by what//database connection 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;//database connection $dbcon = mysql_connect ($dbAddress. ': '. $dbPort, $dbUser, $DBPWD);//Database connection validation if ($dbCon) {// Database connection succeeded//specified database $assign=mysql_select_db ($dbName, $dbCon), if ($assign) {mysql_query ("Set names ' UTF8 '");//Set the character set to use Return Array (' Return ' =>true, ' ps ' = ' database connection succeeded ');} Else{return Array (' Return ' =>false, ' ps ' = ' = ' specified database failed ');}} else{//Database ConnectionThe failed return array (' Return ' =>false, ' ps ' = ' = ' Database connection failed: '. mysql_error ())}} Query all tables 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; }//query all the data with the keyword and replace all tables in the/*table database an array group keywords query the keyword result to replace with what */public function Queryreplace ($table, $keywords = ", $result _keywords= ") {$this->keywords= $keywords; $this->result_keywords= $result _keywords; $arr =array ();// Load return information $index=1;//self-increment//Loop All tables 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);//To here, the database name is $this->dbname table name is $v field name is Fieldname$fieldresult=mysql_query (' select '. $fieldName. ' From ' $v), while ($fieldRow =mysql_fetch_array ($ Fieldresult)) {//Determine if the data content in this field has a keyword to be replaced $fieldvalue= $fieldRow [$fieldName];if (Strpos ($fieldValue, $keywords)!== FALSE) {//Continue execution of the replacement $replaceback=str_replace if it exists ($keyworDS, $result _keywords, $fieldValue);//change data 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;}} Program Logic $replace=new replace ();//Instantiate class $steps=$_get["steps"];//perform steps//dbset database information settings//detection detect 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 ("value with asterisk must be filled in");} $db = $replace->dbconnect ($dbAddress, $dbUser, $DBPWD, $dbName, $dbPort); $queryTable = $replace->querytable ();} The following is html?><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiOnal.dtd ">

UTF-8 version

SOURCE Operation Result:

PHP Bulk replaces data content in MySQL database (replace MySQL database content source code)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.