Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql加载文件中的数据导入零';用php实现十进制数据的s_Php_Mysql_Csv_Import_Null - Fatal编程技术网

mysql加载文件中的数据导入零';用php实现十进制数据的s

mysql加载文件中的数据导入零';用php实现十进制数据的s,php,mysql,csv,import,null,Php,Mysql,Csv,Import,Null,我在将多个文件的csv数据加载到mysql时遇到问题。 当通过phpmyadmin导入csv单独(一次一个)时,数据以十进制值正确显示 但在导入csv文件中的值时,所有列都包含0.00 这是PHP im使用的: // to create the headers foreach($csvtable as $file){ $table = basename($file, '.csv'); mysqli_query($conn, " CREATE TAB

我在将多个文件的csv数据加载到mysql时遇到问题。 当通过phpmyadmin导入csv单独(一次一个)时,数据以十进制值正确显示

但在导入csv文件中的值时,所有列都包含0.00

这是PHP im使用的:

// to create the headers
foreach($csvtable as $file){
    
    $table = basename($file, '.csv'); 

    mysqli_query($conn, "
    CREATE TABLE IF NOT EXISTS ".$csvtable."(
    `colum1` decimal(6,2),
    `colum2` decimal(6,2),
    `colum3` decimal(6,2),
    `colum4` decimal(5,2),
    `colum5` decimal(6,2),
    `colum6` decimal(6,2),
    `colum7` decimal(5,2),
    `colum8` decimal(6,2),
    `colum9` decimal(6,2),
    `colum10` decimal(5,2) 
    )
    ");
// Code to load the data from the files in the tables and ignore the header

mysqli_query($conn, "
    LOAD DATA INFILE '".$file."' INTO TABLE ".$table."
    FIELDS TERMINATED BY ';'
    IGNORE 1 LINES
    ");

csv文件中包含以下内容:

column1;column2;column3;column4;column5;column6;column7;column8;column9;column10
4.50;1.67;0.00;0.00;0.83;0.00;0.00;0.83;0.00;0.00
9.00;3.33;0.00;0.00;1.75;0.00;0.00;1.75;0.00;0.00
13.50;5.00;0.00;0.00;2.58;0.00;0.00;2.58;0.00;0.00
18.00;6.67;0.00;0.00;3.50;0.00;0.00;3.50;0.00;0.00
22.50;8.33;0.00;0.00;4.33;0.00;0.00;4.33;0.00;0.00
27.00;10.08;0.00;0.00;5.25;0.00;0.00;5.25;0.00;0.00
31.50;11.75;0.00;0.00;6.08;0.00;0.00;6.08;0.00;0.00
36.00;13.42;0.00;0.00;7.00;0.00;0.00;7.00;0.00;0.00
40.50;15.08;0.00;0.00;7.83;0.00;0.00;7.83;0.00;0.00
45.00;16.75;0.00;0.00;8.75;0.00;0.00;8.75;0.00;0.00
49.50;18.42;0.00;0.00;9.58;0.00;0.00;9.58;0.00;0.00
但是像这样的负载:

0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00;0.00
这些表是用精确的结构制作的,就像我用phpmyadmin本身导入它一样。
我忘了什么。

您的代码不完整!但是我做了一点改变,把它写在我的身边,并且工作正常。在第一步中,您为板条箱表使用了组名,但将其插入另一个表中。为
创建表
插入表使用相同的名称

<!DOCTYPE html>
<html>
<body>

<?php

$con=mysqli_connect("localhost","user","pass","test");

echo '<br/>';


    $file = "D:\\a.csv";
    
    $table = basename($file, '.csv');
    
    echo '----------'.$table.'</br>';
    
    mysqli_query($con, "
    CREATE TABLE IF NOT EXISTS ".$table."(
    `colum1` decimal(6,2),
    `colum2` decimal(6,2),
    `colum3` decimal(6,2),
    `colum4` decimal(5,2),
    `colum5` decimal(6,2),
    `colum6` decimal(6,2),
    `colum7` decimal(5,2),
    `colum8` decimal(6,2),
    `colum9` decimal(6,2),
    `colum10` decimal(5,2)
    )
    ");
    // Code to load the data from the files in the tables and ignore the header
    
    mysqli_query($con, "
    LOAD DATA INFILE '".$file."' INTO TABLE ".$table."
    FIELDS TERMINATED BY ';'
    IGNORE 1 LINES
    ");
?>

</body>
</html>


哈吉巴巴,我最小化了我的代码。我忘了提到,当我将十进制改为varchar时,在正确的表中导入不会有任何问题。接下来的问题是根据数字对表进行排序。当列设置为十进制时,我没有排序问题。使用十进制很好,无需将其更改为varchar。使用decimal和order by来执行您想要的操作。我并没有将其更改为varchar,但在使用varchar类型将其设置为decimal之前对其进行了测试。正确导入多个文件。每个文件都需要创建自己的表。