Sometimes write a program when the background requires a large amount of data into the database, such as test scores, phone books and so on is generally stored in Excel data, then we can export Excel into a CSV file, and then through the following program can be bulk import data into the database
Upload CVS and import into the database, the test is successful (some of the code is not standardized, such as php_self there to rewrite into $_server["php_self"])
PHP code
The following is the referenced content: $fname = $_files[' MyFile ' [' name ']; $do = Copy ($_files[' MyFile ' [' tmp_name '], $fname); if ($do) { echo "Import data successfully "; } else { echo ""; } ?>
error_reporting (0); Import files in CSV format $connect =mysql_connect ("localhost", "a0530093319", "123456") or Die ("could does connect to database"); mysql_select_db ("a0530093319", $connect) or Die (Mysql_error ()); $fname = $_files[' MyFile ' [' name ']; $handle =fopen ("$fname", "R"); while ($data =fgetcsv ($handle, 10000, ",")) { $q = "INSERT INTO Test (code,name,date) VALUES (' $data [0] ', ' $data [1] ', ' $data [2] ')"; mysql_query ($q) or Die (Mysql_error ());
} Fclose ($handle); ?>
|
Use PHP to export the database to Excel, test completely successful
PHP Code www.devdao.com
The following is the referenced content: $DB _server = "localhost"; $DB _username = "root"; $DB _password = ""; $DB _dbname = "Ishop"; $DB _tblname = "Oi_mall_payment";
$savename = Date ("Ymjhis"); $Connect = @mysql_connect ($DB _server, $DB _username, $DB _password) or Die ("couldn ' t Connect."); mysql_query ("Set Names ' GBK '"); $file _type = "Vnd.ms-excel"; $file _ending = "xls"; Header ("content-type:application/$file _type;charset=big5"); Header ("content-disposition:attachment; Filename= ". $savename.". $file _ending "); Header ("Pragma:no-cache");
$now _date = Date ("Y-m-j h:i:s"); $title = "Database name: $DB _dbname, Data sheet: $DB _tblname, Backup date: $now _date";
$sql = "SELECT * from $DB _tblname"; $ALT _db = @mysql_select_db ($DB _dbname, $Connect) or Die ("couldn ' t Select Database"); $result = @mysql_query ($sql, $Connect) or Die (Mysql_error ());
Echo ("$title \ n"); $sep = "\ t"; for ($i = 0; $i < Mysql_num_fields ($result); $i + +) { Echo Mysql_field_name ($result, $i). "\ t"; } Print ("\ n"); $i = 0; while ($row = Mysql_fetch_row ($result)) { $schema _insert = ""; for ($j =0; $j if (!isset ($row [$j])) $schema _insert. = "NULL". $sep; ElseIf ($row [$j]! = "") $schema _insert. = "$row [$j]". $sep; Else $schema _insert. = "". $sep; } $schema _insert = Str_replace ($sep. " $ "," ", $schema _insert); $schema _insert. = "\ T"; Print (Trim ($schema _insert)); print "\ n"; $i + +; } return (true); ?> |
http://www.bkjia.com/PHPjc/364071.html www.bkjia.com true http://www.bkjia.com/PHPjc/364071.html techarticle sometimes write a program when the background requires a large amount of data into the database, such as test scores, phone books and so on is generally stored in Excel data, then we can export Excel into a CSV file, ...