导入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语句产生
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( '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')
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
答案 0 :(得分:0)
使用mysqli扩展名,因为mysql是deprecated。
addslashes的手册页
为了逃避数据库参数,出于安全原因,应使用DBMS特定的转义函数(例如MySQL的mysqli_real_escape_string()或pg_escape_literal(),PostgreSQL的pg_escape_string())。
对于mysql,请尝试mysql_real_escape_string,对于mysqli,请尝试mysqli_real_escape_string。
我不使用这种格式来检查/显示错误,但我认为你写这个的方式是:
mysql_query($yourInsertQueryString) or die("THere was an issue!!: " . mysql_error());
所有这些列类型都是字符串吗?
答案 1 :(得分:0)
您也可以尝试这种方法,引用字段并使用“couple”分配来验证正确的分配(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
答案 2 :(得分:0)