Sometimes when writing a program, the background requires a large amount of data to be imported into the database. For example, test scores, phone books, etc. are generally data stored in excel. At this time, we can export excel into a csv file, and then use the following program. Batch import data into the database
Upload cvs and import into the database, the test is successful (some codes are not standardized, such as PHP_SELF, which needs to be rewritten as $_SERVER["PHP_SELF"]) PHP code
error_reporting(0); //导入CSV格式的文件 $connect=mysql_connect("localhost","a0530093319","123456") or die("could not 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); ?>
$ 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 not 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 ); <🎜>?>
Used php to export the database to excel, the test was completely successful PHP code www.devdao.com
http://www.bkjia.com/PHPjc/364071.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364071.htmlTechArticleSometimes when writing a program, the background requires importing a large amount of data into a database, such as test scores, phone books, etc., which are generally stored For data in excel, we can export excel into a csv file...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn