从csv文件中读取第一行并根据它在mysql中自动创建表(csv文件字段).寻找PHP脚本?
我试过这个
PHP
$arr = array(array(),array());
$num = 0;
$row = 0;
$handle = fopen("./contacts.csv","r");
while($data = fgetcsv($handle,1000,",")){
$num = count($data);
for ($c=0; $c < $num; $c++) {
$arr[$row][$c] = $data[$c];
}
$row++;
}
$con = MysqL_connect('localhost','root','');
MysqL_select_db("excel_database",$con);
for($i=1; $i<$row; $i++){
$sql = "INSERT INTO contacts VALUES ('".$arr[$i][0]."','".$arr[$i][1]."','".$arr[$i][2]."','".$arr[$i][3]."','".$arr[$i][4]."','".$arr[$i][5]."')";
MysqL_query($sql,$con);
}
?>
最佳答案
这是你问题的一半.
PHP
$row = 1;
if (($handle = fopen("ab.csv","r")) !== FALSE) {
while (($data = fgetcsv($handle,")) !== FALSE) {
$num = count($data);
echo "