这篇文章主要介绍了php将csv文件导入到mysql数据库的方法,通过读取csv文件到数组再调用while循环实现插入数据到数据库,是非常实用的技巧,需要的朋友可以参考下
本程序实现数据导入原理是先把csv文件上传到服务器,然后再通过php的fopen与fgetcsv文件把数据保存到数组,然后再用while把数据一条条插入到mysql数据库,代码如下:
<?php $fname = $_files['myfile']['name']; $do = copy($_files['myfile']['tmp_name'],$fname); if ($do){ echo"导入数据成功<br>"; }else{ echo ""; } error_reporting(0);// 导入csv格式的文件 $connect=mysql_connect("localhost","root","") or die("could not connect to database"); mysql_select_db("gklqtzcx",$connect) or die (mysql_error()); mysql_query("set names 'gbk'"); $fname = $_files['myfile']['name']; $handle=fopen("$fname","r"); while($data=fgetcsv($handle,10000,",")){ $q="insert into records (name,classes,a_time,college,notify,receiver,r_time,handler) values ('$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')"; mysql_query($q) or die (mysql_error()); } fclose($handle); echo "<meta http-equiv="refresh" content="1;url=list.php">1秒钟转入列表页,请稍等."; ?> <form enctype="multipart/form-data" action="<?php echo"".$_server["php_self"].""; ?>" method="post"> <p>导入cvs数据 <input name="myfile" type="file"> <input value="提交" type="submit"> </p> </form>
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
