Php implements mysql synchronization. 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. So after trying to use C to get 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
The code is as follows:
/**
* 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:
The code is as follows:
/**
* 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:
". 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"