PHP編程實(shí)現(xiàn)csv文件導(dǎo)入mysql數(shù)據(jù)庫的方法
本文實(shí)例講述了PHP編程實(shí)現(xiàn)csv文件導(dǎo)入mysql數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
config.db.php內(nèi)容如下;
<?php
$username="root";
$userpass="123";
$dbhost="localhost";
$dbdatabase="credits2stakes";
//生成一個(gè)連接
$db_connect=mysql_connect($dbhost,$username,$userpass) or die("Unable to connect to the MySQL!");
//選擇一個(gè)需要操作的數(shù)據(jù)庫
mysql_select_db($dbdatabase,$db_connect);
index.php內(nèi)容如下:
<meta http-equiv="Content-Type"content="text/html; charset=utf-8" /> <form name="frm1" enctype="multipart/form-data" action="insertdb.php" method="post"> <input name="filename" type="file" /><input name="submit" type="submit" value="import" /> </form>
insertdb.php內(nèi)容如下:
<?php
session_start();
header("Content-type:text/html;charset:utf-8");
//全局變量
$file=$_FILES['filename'];
$max_size="2000000"; //最大文件限制(單位:byte)
$fname=$file['name'];
$ftype=strtolower(substr(strrchr($fname,'.'),1));
//文件格式
$uploadfile=$file['tmp_name'];
if($_SERVER['REQUEST_METHOD']=='POST'){
if(is_uploaded_file($uploadfile)){
if($file['size']>$max_size){
echo "Import file is too large";
exit;
}
if($ftype!='csv'){
echo "Import file type is error";
exit;
}
}else{
echo "The file is not empty!";
exit;
}
}
require("./config.db.php"); //連接mysql數(shù)據(jù)庫
$row=0;
$filename=$file['tmp_name'];
$handle=fopen($filename,'r');
while(!feof($handle) && $data=fgetcsv($handle,1000,',')){
$arr_result=array();
if($row==0){
$row++;
continue;
}
if($row>0 && !empty($data)){
$num=count($data);
for($i=0;$i<$num;$i++){
array_push($arr_result,$data[$i]);
}
//$name = iconv('gb2312','utf-8',$arr_result[1]);
//$sex = iconv('gb2312','utf-8',$arr_result[2]);
$sql="insert into inviter(inviter,invitees,time) value($arr_result[1],$arr_result[2],$arr_result[3])";
//echo $sql;
mysql_query("set names utf8");
$result=mysql_query($sql);
if($result){
echo "插入成功?。。?;
}else{
echo "插入失?。。。?;
}
}
$row++;
}
fclose($handle);
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- Navicat把csv數(shù)據(jù)導(dǎo)入mysql
- Navicat for MySql可視化導(dǎo)入CSV文件
- 使用navicat將csv文件導(dǎo)入mysql
- mysql實(shí)現(xiàn)查詢結(jié)果導(dǎo)出csv文件及導(dǎo)入csv文件到數(shù)據(jù)庫操作
- Python之csv文件從MySQL數(shù)據(jù)庫導(dǎo)入導(dǎo)出的方法
- Python實(shí)現(xiàn) 多進(jìn)程導(dǎo)入CSV數(shù)據(jù)到 MySQL
- php將csv文件導(dǎo)入到mysql數(shù)據(jù)庫的方法
- mysql使用SQLyog導(dǎo)入csv數(shù)據(jù)不成功的解決方法
- Mysql 導(dǎo)入導(dǎo)出csv 中文亂碼問題的解決方法
- MySql導(dǎo)入CSV文件或制表符分割的文件
相關(guān)文章
PHP的命令行擴(kuò)展Readline相關(guān)函數(shù)的使用
PHP 作為一個(gè) Web 開發(fā)語言,相對(duì)來說,命令行程序并不是它的主戰(zhàn)場(chǎng)。所以很多年輕的 PHP 開發(fā)者可能連命令行腳本都沒有寫過,更別提交互式的命令操作了。而今天,我們帶來的這個(gè)擴(kuò)展就是針對(duì) PHP 的交互式命令行操作的2021-05-05
php基于 swoole 實(shí)現(xiàn)的異步處理任務(wù)功能示例
這篇文章主要介紹了php基于 swoole 實(shí)現(xiàn)的異步處理任務(wù)功能,涉及swoole的安裝、配置及php調(diào)用swoole守護(hù)進(jìn)程監(jiān)控任務(wù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
php錯(cuò)誤提示failed to open stream: HTTP request failed!的完美解決方法
當(dāng)使用php5自帶的file_get_contents方法來獲取遠(yuǎn)程文件的時(shí)候,有時(shí)候會(huì)出現(xiàn)file_get_contents(): failed to open stream: HTTP request failed!這個(gè)警告信息。2011-06-06
php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實(shí)現(xiàn)方法,涉及針對(duì)表單的處理與sql語句的靈活使用,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
php 刪除一個(gè)數(shù)組中的某個(gè)值.兼容多維數(shù)組!
php中刪除一個(gè)數(shù)組中的某個(gè)值.兼容多維數(shù)組,需要的朋友可以參考下2012-02-02

