Using PHP to take the first line of a CSV file and create a MySQL Table with the data

前端 未结 4 1436
忘掉有多难
忘掉有多难 2021-01-03 15:08

I am trying to take a rather large CSV file and insert it into a MySQL database for referencing in a project. I would like to use the first line of the file to create the ta

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 15:50

    \n";
    for($i = 0; $i <= count($fieldList); $i++)
    {
    
        if(strlen($fieldList[$i]) == 0) $typeInfo = 'varchar(80)';
        if(preg_match('/[0-9]/', $fieldList[$i])) $typeInfo = 'int(11)';
        if(preg_match('/[\.]/', $fieldList[$i])) $typeInfo = 'float';
        if(preg_match('/[a-z\\\']/i', $fieldList[$i])) $typeInfo = 'varchar(80)';
    
        echo "\t".'`'.$i.'` '.$typeInfo.' NOT NULL, -- '.gettype($fieldList[$i]).' '.$fieldList[$i]."
    \n"; } echo ' PRIMARY KEY (`0`)'."
    \n"; echo ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;';

提交回复
热议问题