function export_csv( $table, // $afields, // - $filename, // CSV // ( web-) $delim=',', // CSV $enclosed='"', // $escaped='\\', // $lineend='\\r\\n'){ // CSV $q_export = "SELECT ".implode(',', $afields). " INTO OUTFILE '".$_SERVER['DOCUMENT_ROOT'].$filename."' ". "FIELDS TERMINATED BY '".$delim."' ENCLOSED BY '".$enclosed."' ". " ESCAPED BY '".$escaped."' ". "LINES TERMINATED BY '".$lineend."' ". "FROM ".$table ; // , if(file_exists($_SERVER['DOCUMENT_ROOT'].$filename)) unlink($_SERVER['DOCUMENT_ROOT'].$filename); return mysql_query($q_export); }
GRANT FILE ON * . * TO 'username'@'localhost' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
function import_csv( $table, // $afields, // - $filename, // CSV , // ( web-) $delim=',', // CSV $enclosed='"', // $escaped='\\', // $lineend='\\r\\n', // CSV $hasheader=FALSE){ // CSV if($hasheader) $ignore = "IGNORE 1 LINES "; else $ignore = ""; $q_import = "LOAD DATA INFILE '". $_SERVER['DOCUMENT_ROOT'].$filename."' INTO TABLE ".$table." ". "FIELDS TERMINATED BY '".$delim."' ENCLOSED BY '".$enclosed."' ". " ESCAPED BY '".$escaped."' ". "LINES TERMINATED BY '".$lineend."' ". $ignore. "(".implode(',', $afields).")" ; return mysql_query($q_import); }
$afields
or using a subquery instead of the table name (then the array will contain the fields of this subquery) - for example, $atable
will look like this (select field1, field1 from table2) t
array("column1", "@dummy", "column2", "@dummy", "column3")
will skip the second and fourth fields in the CSV file.Source: https://habr.com/ru/post/115433/
All Articles