0

导入 CSV 的默认 phpmyadmin 功能对我不起作用(旋转和旋转没有错误),所以我编写了一个脚本来导入它。我的脚本没有收到任何错误,但没有任何行正在导入!你能看出我做错了什么吗?

<?php

$db_host        = 'secret';
$db_user        = 'secret';
$db_pass        = 'secret';
$db_database    = 'secret'; 

/* End config */



$link = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');
echo "Successfully connected \n";
mysql_select_db($db_database,$link);
mysql_query("SET names UTF8");

//get the csv file
   // $file = $_FILES[csv][dc.csv];
$handle = fopen("dc.csv","r");
$counter = 0;    
$data = array();






    echo "starting import \n";
    while( ($line = fgetcsv($handle)) !== false) {
    $data[] = $line;


        //if ($data[0]) {
           mysql_query("INSERT INTO private_schools (school_name,
                                                      city,
                                                      state,
                                                      county_code,
                                                      zip_code,
                                                      NA2,
                                                      low_grade,
                                                      high_grade,
                                                      NA3,
                                                      NA4,
                                                      NA5,
                                                      grade_range,
                                                      NA6,
                                                      NA7,
                                                      NA8,
                                                      NA9,
                                                      NA10,
                                                      NA11,
                                                      NA12,
                                                      NA13,
                                                      NA14,
                                                      NA15,
                                                      NA16) VALUES
                (
                    '".addslashes($data[$counter][0])."',
                    '".addslashes($data[$counter][1])."',
                    '".addslashes($data[$counter][2])."',
                    '".addslashes($data[$counter][3])."',
                    '".addslashes($data[$counter][4])."',
                    '".addslashes($data[$counter][5])."',
                    '".addslashes($data[$counter][6])."',
                    '".addslashes($data[$counter][7])."',
                    '".addslashes($data[$counter][8])."',
                    '".addslashes($data[$counter][9])."',
                    '".addslashes($data[$counter][10])."',
                    '".addslashes($data[$counter][11])."',
                    '".addslashes($data[$counter][12])."',
                    '".addslashes($data[$counter][13])."',
                    '".addslashes($data[$counter][14])."',
                    '".addslashes($data[$counter][15])."',
                    '".addslashes($data[$counter][16])."',
                    '".addslashes($data[$counter][17])."',
                    '".addslashes($data[$counter][18])."',
                    '".addslashes($data[$counter][19])."',
                    '".addslashes($data[$counter][20])."',
                    '".addslashes($data[$counter][21])."',
                    '".addslashes($data[$counter][22])."'
                )
            " or die("THere was an issue!!: " . mysql_error()));
            //print($data[$counter][0]);
            print "INSERT INTO private_schools (school_name,
                                                      city,
                                                      state,
                                                      county_code,
                                                      zip_code,
                                                      NA2,
                                                      low_grade,
                                                      high_grade,
                                                      NA3,
                                                      NA4,
                                                      NA5,
                                                      grade_range,
                                                      NA6,
                                                      NA7,
                                                      NA8,
                                                      NA9,
                                                      NA10,
                                                      NA11,
                                                      NA12,
                                                      NA13,
                                                      NA14,
                                                      NA15,
                                                      NA16) VALUES
                (
                    '".addslashes($data[$counter][0])."',
                    '".addslashes($data[$counter][1])."',
                    '".addslashes($data[$counter][2])."',
                    '".addslashes($data[$counter][3])."',
                    '".addslashes($data[$counter][4])."',
                    '".addslashes($data[$counter][5])."',
                    '".addslashes($data[$counter][6])."',
                    '".addslashes($data[$counter][7])."',
                    '".addslashes($data[$counter][8])."',
                    '".addslashes($data[$counter][9])."',
                    '".addslashes($data[$counter][10])."',
                    '".addslashes($data[$counter][11])."',
                    '".addslashes($data[$counter][12])."',
                    '".addslashes($data[$counter][13])."',
                    '".addslashes($data[$counter][14])."',
                    '".addslashes($data[$counter][15])."',
                    '".addslashes($data[$counter][16])."',
                    '".addslashes($data[$counter][17])."',
                    '".addslashes($data[$counter][18])."',
                    '".addslashes($data[$counter][19])."',
                    '".addslashes($data[$counter][20])."',
                    '".addslashes($data[$counter][21])."',
                    '".addslashes($data[$counter][22])."'
                )
            " ;
            $counter ++;
            if ($counter === 3){
            exit();
            }
       // }
    }

//var_dump($data);
?>

打印 sql 语句产生

插入私立学校(学校名称、城市、州、县代码、邮政编码、NA2、low_grade、high_grade、NA3、NA4、NA5、grade_range、NA6、NA7、NA8、NA9、NA10、NA11、NA12、NA13、NA14、NA15、NA16)值('福音古典基督教学校','雪花石膏','AL','1117','35007','21','KG','12','5','2','8',' K-12'、'295'、'40.7'、'28.8'、'10.2'、'99'、'0.7'、'0'、'0'、'0'、'0'、'0.3')

CSV 文件示例:

Abbeville Christian Academy,Abbeville,AL,1067,36310,42,PK,12,6,2,8,K-12,157,52.9,16.6,9.5,95.5,0.6,0,1.9,0,0.6,1.3
Kingwood Christian School,Alabaster,AL,1117,35007,21,KG,12,6,2,4,K-12,413,44.1,33.1,12.5,88.9,6.8,0,1.9,0,0.2,2.2
Evangel Classical Christian School,Alabaster,AL,1117,35007,21,KG,12,5,2,8,K-12,295,40.7,28.8,10.2,99,0.7,0,0,0,0,0.3
Westwood Bapt Weekday Edu Ministry,Alabaster,AL,1117,35007,21,PK,KG,6,2,5,K-6,12,50,1,12,100,0,0,0,0,0,0
4

3 回答 3

0

您也可以尝试这种方法,也引用字段并使用“夫妇”评估来验证正确的分配(csv 导入的常见问题)

<?php
$db_host        = 'secret';
$db_user        = 'secret';
$db_pass        = 'secret';
$db_database    = 'secret'; 

$_db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_database,$_db);

$fileds = array(

"school_name",
"city",
"state",
"county_code",
"zip_code",
"NA2",
"low_grade",
"high_grade",
"NA3",
"NA4",
"NA5",
"grade_range",
"NA6",
"NA7",
"NA8",
"NA9",
"NA10",
"NA11",
"NA12",
"NA13",
"NA14",
"NA15",   
"NA16",
);

$handle = fopen("dc.csv","r");
$counter = 0;
$data = array();
echo "starting import \n";
while( ($line = fgetcsv($handle)) !== false) {
    $data[] = $line;
}
foreach($data as $row){
    if(count($fileds) != count($row)){
        continue;
    }
    $couples = array();
    foreach($fileds as $k => $v){
        $couples[] = sprintf("`%s` = '%s",$v ,mysql_real_escape_string($row[$k],$_db));
    }
    $_sql = 'INSERT INTO private_schools '.join(',', $couples).';';
    echo "$_sql\n";
    mysql_query($_sql);

}
// and remeber to commit if you use a transactiona storage engine
mysql_query('commit',$_db); // fixed for "old" client
于 2013-08-01T17:13:15.973 回答
0

考虑使用mysqli扩展,因为 mysql 已弃用

addlashes 的联机帮助

为了对数据库参数进行转义,出于安全原因,应使用 DBMS 特定的转义函数(例如 MySQL 的 mysqli_real_escape_string() 或 PostgreSQL 的 pg_escape_literal()、pg_escape_string())。

对于 mysql 尝试mysql_real_escape_string,对于 mysqli 尝试mysqli_real_escape_string

我不使用这种格式来检查/显示错误,但我认为你写这个的方式是:

mysql_query($yourInsertQueryString) or die("THere was an issue!!: " . mysql_error());

所有这些列类型都是字符串吗?

于 2013-08-01T16:52:50.350 回答
0

您可以尝试使用 mysqlimport。您从命令窗口运行。

这是一个很好的简单教程

如果您使用的是 windows 并不重要,它也可以在上面使用,只是一样。

于 2013-08-01T17:18:30.933 回答