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

前端 未结 4 1438
忘掉有多难
忘掉有多难 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:30

    Try casting the value and comparing it with the original one:

    define('DECIMAL_SEPARATOR', '.');
    
    switch ($fieldList[$i])
    {
        case (string)(int)$fieldList[$i]:
            $typeInfo = (strpos($fieldList[$i], DECIMAL_SEPARATOR) === false) ? 'int(11)' : 'float';
            break;
        case (string)(float)$fieldList[$i]:
            $typeInfo = 'float';
            break;
        default:
            $typeInfo = 'varchar(80)';
            break;
    }
    

    Additionaly, check for the presence of decimal separator in the first case for numbers that are round, yet they have the decimal fraction part.

提交回复
热议问题