使用PHP代码在MySql中导入CSV文件

时间:2016-10-05 08:13:11

标签: php mysql csv import

我将使用PHP代码在MySql数据库的表中导入CSV文件。 CSV文件如下:

“2016-09-02”,“100.01”,“4005.09”,“5000”,“1.09”,“120.09”,“100.5”,“200.77” “2016-09-03”,“150.01”,“4205.09”,“5600”,“1.10”,“150.09”,“300.5”,“300.77”

文件权限为755。

表字段为9(包括id字段):firts是日期时间字段,其他是浮点字段。 我使用的代码如下,它将在服务器站点上运行:

$csvFile = "../scripts/tabella.csv";

$db = @mysql_connect('**.***.**.***', 'Sql******', '*******');
@mysql_select_db('Sql******_*');

$query = 'LOAD DATA LOCAL INFILE \' '. $csvFile .' \' INTO TABLE mytable FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' ';

if(!mysql_query($query)){
    die(mysql_error());
}
mysql_close($db);

从mysql_error返回第一个错误:'找不到文件'。 CSV文件位于“www.mysite.it/mysite/scripts/tabella.CSV”中。它的权限是755.我尝试使用realpath($ csvFile)函数,但错误始终是相同的。

我尝试在localhost中运行相同的查询,并且没有此错误,但只有一条记录插入到表中,其fild值为NULL。

你能帮助我吗? 谢谢!

4 个答案:

答案 0 :(得分:0)

使用mysql_error获取错误

 if(!mysql_query($query)){
      die(mysql_error());

 }

答案 1 :(得分:0)

尝试以下代码..

$csvFile = "../scripts/tabella.csv";

//The Connection
$mysqli = new mysqli($host,$username,$password,$database);

//Check for successful connection
if ($mysqli->connect_errno) echo "Error - Failed to connect to MySQL: " . $mysqli->connect_error; die;

$query = 'LOAD DATA LOCAL INFILE \' '. $csvFile .' \' INTO TABLE mytable FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' ';
//Do your query
$result = mysqli_query($mysqli,$query);

//Close the connection
mysqli_close($mysqli);

答案 2 :(得分:0)

尝试这种简单的方法将CSV数据导入Mysql数据库。file是一个HTML元素名称browse您的文件路径。

if(isset($_POST["submit"]))
    {
        $file = $_FILES['file']['tmp_name'];
        $handle = fopen($file, "r");
        $c = 0;
        while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
        {
            $name= $filesop[0];

            $sql = mysql_query("INSERT INTO tab(name) VALUES ('$name')");
            $c = $c + 1;
        }

        if($sql){
            echo "You database has imported successfully. You have inserted ". $c ." recoreds";
        }else{
            echo "Sorry! There is some problem.";
        }
    }

答案 3 :(得分:0)

尝试此编码有效

<?php 
    $connect = mysqli_connect("localhost", "root", "", "testing");
    if(isset($_POST["submit"]))
{
    if($_FILES['file']['name'])
    {
        $filename = explode(".", $_FILES['file']['name']);
        if($filename[1] == 'csv')
        {
            $handle = fopen($_FILES['file']['tmp_name'], "r");
            while($data = fgetcsv($handle))
            {
                $item1 = mysqli_real_escape_string($connect, $data[0]);  
                $item2 = mysqli_real_escape_string($connect, $data[1]);
                $query = "INSERT into excel(excel_name, excel_phone) values('$item1','$item2')";
                mysqli_query($connect, $query);
            }
            fclose($handle);
            echo "<script>alert('Import done');</script>";
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
    <form method="post" enctype="multipart/form-data">
        <div align="center">
            <label>Select CSV File:</label>
            <input type="file" name="file" />
            <br />
            <input type="submit" name="submit" value="Import" class="btn btn-info" />
        </div>
    </form>
</body>
</html>