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