The realization method of MySQL synchronization

Source: Internet
Author: User


<?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;
}
?>


<?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);
}
}
?>


<?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>

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.