$handle = fopen("test.csv", "r"); // Read first (headers) record only) $data = fgetcsv($handle, 1000, ","); $sql= 'CREATE TABLE table_name ('; for($i=0;$i<count($data); $i++) { $sql .= $data[$i].' VARCHAR(50), '; } //The line below gets rid of the comma $sql = substr($sql,0,strlen($sql)-2); $sql .= ')'; echo $sql; fclose($handle);
Reference: https://bytes.com/topic/mysql/answers/746696-create-mysql-table-field-headings-line-csv-file