How to Implement mysql synchronization in php

Source: Internet
Author: User

After obtaining the requirement, I found that the MYSQL databases of both websites cannot be remotely accessed (security first ). This reminds me of the CSV files used for batch data input. Therefore
Try to import and export data using CSV.
The import framework is as follows:
1. First, export the data to the CSV format.
Create a file and place it on a Chinese server: csv. php. is actually an export function. Data is obtained through databases, table names, and SQL statements.
Csv. php Copy codeThe Code is as follows: <? Php
/**
* Output a table in the database to a CSV file.
*
* @ Param string host of the Mysql database
* @ Param string Database Name
* @ Param string name of the table in the database
* @ Param string connection username of the database
* @ Param string database connection password
* @ Param string name of the database table
* @ Param string
* @ 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 ("the database cannot be connected. 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 the 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"; // used to replace NULL values
} Elseif (row [j] = '0' | row [j]! = ''){
// Loic1: Enclose the field value with 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
// Fwrite (fp, schema_insert. csv_terminated );
Cc. = schema_insert. csv_terminated;
} // End while
Mysql_free_result (result );
// Fclose (fp );
Return cc;
}
?>

2. Import contents in CSV format to the table
Create an imported file on the U.S. server and place it in import. php. The Code is as follows:Copy codeThe Code is as follows: <? Php
/**
* Import data from an uploaded file to a table
*
* @ Param string host of the Mysql database
* @ Param string Database Name
* @ Param string name of the table in the database
* @ Param string connection username of the database
* @ Param string database connection password
* @ Param string name of the database table
*
* @ Return whether bool is successfully executed
*
* @ Access public
*/
Function uploadFileOfCsv (host, db, user, pass, table, content ){
Mysql_connect (host, user, pass) or die ("the database cannot be connected. 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 ());
Rownum = 0;
Log ("the extracted data is 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 (the ID of the 'th". I. "line is empty. Please check it! ');";
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. Call the function to execute and export
Create another file test_export.php on the Chinese server, call the preceding csv. php function, convert the data to CSV, and save it to
Note the position where the form is submitted in texsag:Copy codeThe Code is as follows: <? Php
Require_once ("csv. php ");
Host = "localhost ";
Db = "project ";
User = "root ";
Pass = "";
// Export the data of the tb_contact table as a 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: // address of the US website/test2.php">
<P>
<Textarea name = "textarea" cols = "180" rows = "30"> <? Php echo cc?> </Textarea>
<Input type = "hidden" name = "action" value = "1"/>
</P>
<P>
<Input type = "submit" name = "Submit" value = "submit">
</P>
</Form>

The following file is used to accept uploaded data on the U.S. server. The file name is test_import.php:Copy codeThe Code is as follows: <? Php
Require_once ("csv. php ");
Require_once ("import. php ");
Host = "localhost ";
Db = "wintopweb ";
User = "root ";
Pass = "";
If (_ POST ['action'] = "1 "){
Content = _ POST ['textea '];
Echo uploadFileOfCsv (host, db, user, pass, "tb_project_dvp", content );
}
?>

Finally, use the Windows-xp/nt/03 Control Panel to schedule and execute the Chinese server test_export.php file.

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.