Implementation method of MySQL synchronization in PHP _php skills

Source: Internet
Author: User
Tags error code
After you get the demand, you will find that the MySQL database for both sites cannot be accessed remotely (security first). Then I think of the usual use of CSV file bulk input data. So
Try importing the export using CSV.
The import framework is everywhere as follows:
1 first export the data into a CSV format.
Set up a file to be placed on a Chinese server: csv.php. It's actually an export function that obtains data through databases, table names, and SQL statements.
csv.php
Copy Code code as follows:

<?php
/**
* Output a table in a database into a CSV file
*
* @param string MySQL database host
* @param string database name
* @param the table name in the string database
* @param the connection user name of the string database
* @param the connection password for the string database
* @param the table name of the string database
* @param the string database
* @param string Error page
* @param String SQL statement
*
* @return Text returns the content in CSV format
*
* @access Public
*/
function Pma_exportdata (host,db,user,pass,filename,table, CRLF, Error_url, Sql_query) {
what= "CSV";
Csv_terminated= "";
Csv_separator= ",";
Csv_enclosed= "";
Csv_escaped= "";
Mysql_connect (host, User,pass) or Die ("Cannot connect to the database, the error code is as follows:". Mysql_error ());
mysql_select_db (DB);
result = mysql_query (sql_query);
fields_cnt = Mysql_num_fields (result);
Cc= "";
fp = fopen (filename, ' w ');
Format data
while (row = mysql_fetch_row (result)) {
Schema_insert = ';
for (j = 0; J < fields_cnt; J + +) {
if (!isset (row[j)) | | | is_null (ROW[J])) {
Schema_insert. = "NULL"; What to replace null values with
} elseif (Row[j] = = ' 0 ' | | | row[j]!= ') {
Loic1: Include field values in quotation marks
if (csv_enclosed = = "") {
Schema_insert. = Row[j];
} else {
Schema_insert. = csv_enclosed
. Str_replace (csv_enclosed, csv_escaped. csv_enclosed, Row[j])
. csv_enclosed;
}
} else {
Schema_insert. = ';
}
if (J < fields_cnt-1) {
Schema_insert. = Csv_separator;
}
}//End for
Fwrite (Fp,schema_insert. csv_terminated);
Cc.=schema_insert. csv_terminated;
}//End While
Mysql_free_result (result);
Fclose (FP);
return cc;
}
?>

2. Import the content in CSV format into the table
To create an imported file on a U.S. server, place: import.php, code as follows:
Copy Code code as follows:

<?php
/**
* Import data from an uploaded file into a table
*
* @param string MySQL database host
* @param string database name
* @param the table name in the string database
* @param the connection user name of the string database
* @param the connection password for the string database
* @param the table name of the string database
*
* @return BOOL executed successfully
*
* @access Public
*/
function Uploadfileofcsv (host,db,user,pass,table,content) {
Mysql_connect (host, User,pass) or Die ("Cannot connect to the database, the error code is as follows:". Mysql_error ());
mysql_select_db (DB);
result = mysql_query ("Select * from table");
fields_cnt = Mysql_num_fields (result);
Test2=array (Array ());
rownum=0;
Log ("Extracted data as follows:<br>". Content);
FD1 = fopen ("C:test.csv", ' a ');
Fwrite (fd1,content);
Fclose (FD1);
fp = fopen ("C:test.csv", "R");
while (buffer = fgets (fp,4096))
{
i++;
Tmp_arr = Explode (",", buffer);
if (Trim (tmp_arr[0]) = = "") {
echo "<script language= ' JavaScript ' >";
echo "Alert" (' first ". The ID of the row is empty, please check! ');";
echo "Location.href=document.referrer;";
echo "</script>";
Exit
}
query = "INSERT into db.table";
Query. = "VALUES (";
for (q=0;q<fields_cnt;q++) {
if (q==fields_cnt-1) {
TMP=TMP_ARR[Q];
query.= "' tmp ');";
}else{
TMP=TMP_ARR[Q];
query.= "' tmp ',";
}
}//end for (q=0;
log2 (query);
mysql_query (query);
}
Fclose (FP);
return "OK";
Unlink ("C:test.csv");
}
function log2 (event = null) {
Global DB;
Global login;
if (log_enabled) {
now = Date ("y-m-d h:i:s");
FD = fopen ("c:log.html", ' a ');
Log = Now. " ". _server[" REMOTE_ADDR "]."-Event <br> ";
Fwrite (Fd,log);
Fclose (FD);
}
}
?>

3 calling function to perform export
Create a file on a Chinese server: test_export.php, call the previous csv.php function, and then convert the data to CSV and then temporarily save it to a form's
Textera, note the location of the form submission:
Copy Code code as follows:

<?php
Require_once ("csv.php");
host= "localhost";
db= "Project";
User= "Root";
Pass= "";
Export tb_contact table data to CSV file
filename = ' file4.csv ';
Cc=pma_exportdata (host,db,user,pass, filename, "TB_PROJECT_DVP", "", "test.php", "SELECT * from TB_PROJECT_DVP");
handle = fopen (filename, "RB");
Contents = fread (handle, filesize (filename));
Fclose (handle);
?>
<form id= "Form1" Name= "Form1" method= "post" action= "http://U.S. website address/test2.php" >
<p>
<textarea name= "textarea" cols= 180 "rows=" ><?php Echo cc?></textarea>
<input type= "hidden" name= "Action" value= "1"/>
</p>
<p>
<input type= "Submit" name= "Submission" value= "submitted" >
</p>
</form>

Again on the United States server to prevent the following files to accept uploaded data, file name is test_import.php:
Copy Code code as follows:

<?php
Require_once ("csv.php");
Require_once ("import.php");
host= "localhost";
db= "Wintopweb";
User= "Root";
Pass= "";
if (_post[' action ']== "1") {
content=_post[' textarea '];
Echo uploadfileofcsv (Host,db,user,pass, "TB_PROJECT_DVP", content);
}
?>

Finally, using the Windows-xp/nt/03 control surface version with the task plan, the dispatch executes the Chinese server test_export.php file can
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.