猿问

使用PHP / MySQL导入CSV数据

尝试从CSV导入数据时遇到了一些问题,但还有一些问题,我还没有解决。


首先,这是我的代码,可以帮助您更好地了解事物(将其整理一些,删除CSS和DB连接):


<body>

<div id="container">

<div id="form">


<?php

$deleterecords = "TRUNCATE TABLE tablename"; //empty the table of its current records

mysql_query($deleterecords);


//Upload File

if (isset($_POST['submit'])) {


    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {

        echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded 

 successfully." . "</h1>";

        echo "<h2>Displaying contents:</h2>";

        readfile($_FILES['filename']['tmp_name']);

    }


    //Import uploaded file to Database

    $handle = fopen($_FILES['filename']['tmp_name'], "r");


    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        $import="INSERT into importing(text,number)values('$data[0]','$data[1]')";


        mysql_query($import) or die(mysql_error());

    }


    fclose($handle);


    print "Import done";


//view upload form

} else {


    print "Upload new csv by browsing to file and clicking on Upload<br />\n";


    print "<form enctype='multipart/form-data' action='upload.php' method='post'>";


    print "File name to import:<br />\n";


    print "<input size='50' type='file' name='filename'><br />\n";


    print "<input type='submit' name='submit' value='Upload'></form>";


}


?>


</div>

</div>

</body>

从本质上讲,这是我对各种方法进行多次尝试后发现的示例的改编。


我的CSV有两列数据,第一列是文本,第二列是整数数据库中的表也有两列,第一列称为“文本”,第二列为“数字”


所以我的问题是:


正在上传的文本只是在每个字段中显示为0,我不确定为什么

我一直在阅读有关以“”括起来的数据的信息,如果发生这种情况,我将如何对其进行排序?

如何忽略标题等的CSV的前X行?

在整个过程中数据格式是否已更改,或者是否可以在图形中使用?例如,一旦放入数据库,小数点会保留小数点吗?

我想涵盖了所有内容,在此先感谢您的帮助!


编辑:


刚刚对10,000条记录上传进行了测试,但出现错误:


“致命错误:超过30秒的最大执行时间”


有什么想法吗?


精慕HU
浏览 650回答 3
3回答

MMMHUHU

$i=0;while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {if($i>0){&nbsp; &nbsp; $import="INSERT into importing(text,number)values('".$data[0]."','".$data[1]."')";&nbsp; &nbsp; mysql_query($import) or die(mysql_error());}$i=1;}

料青山看我应如是

letsay $ infile = a.csv //需要导入文件。class blah{&nbsp;static public function readJobsFromFile($file){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (($handle = fopen($file, "r")) === FALSE)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo "readJobsFromFile: Failed to open file [$file]\n";&nbsp; &nbsp; &nbsp; &nbsp; die;&nbsp; &nbsp; }&nbsp; &nbsp; $header=true;&nbsp; &nbsp; $index=0;&nbsp; &nbsp; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // ignore header&nbsp; &nbsp; &nbsp; &nbsp; if ($header == true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $header = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ($data[0] == '' && $data[1] == '' ) //u have oly 2 fields&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "readJobsFromFile: No more input entries\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $a&nbsp; &nbsp; &nbsp; = trim($data[0]);&nbsp; &nbsp; &nbsp; &nbsp; $b&nbsp; &nbsp;= trim($data[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (check_if_exists("SELECT count(*) FROM Db_table WHERE a='$a' AND b='$b'") === true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $sql = "INSERT INTO DB_table SET a='$a' , b='$b' ";&nbsp; &nbsp; &nbsp; &nbsp; @mysql_query($sql) or die("readJobsFromFile: " . mysql_error());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $index++;&nbsp; &nbsp; }&nbsp; &nbsp; fclose($handle);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return $index; //no. of fields in database.}&nbsp;functioncheck_if_exists($sql){$result = mysql_query($sql) or die("$sql --" . mysql_error());if (!$result) {&nbsp; &nbsp; $message&nbsp; = 'check_if_exists::Invalid query: ' . mysql_error() . "\n";&nbsp; &nbsp; $message .= 'Query: ' . $sql;&nbsp; &nbsp; die($message);}$row = mysql_fetch_assoc ($result);$count = $row['count(*)'];if ($count > 0)&nbsp; &nbsp; return true;return false;}$infile=a.csv;&nbsp;blah::readJobsFromFile($infile);}希望这可以帮助。
随时随地看视频慕课网APP
我要回答