导入 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