Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PHP Create MySQL Table From CSV File Headings

Posted on November 5, 2015 By David Kittell
$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

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MySQL PHP SQL

Post navigation

Previous post
Next post

Related Posts

Ektron Content Type

Posted on October 21, 2013October 26, 2015

Public Function GetContentType(ByVal sId As String) As String Select Case [sId] Case "1" Return "Content" Case "2" Return "Forms" Case "3" Return "Archive Content" Case "4" Return "Archive Forms" Case "7" Return "Library Item" Case "8" Return "Assets" Case "9" Return "Archive Assets" Case "12" Return "Archive Media" Case "99"…

Read More

Medicare Health Assessment Report

Posted on August 8, 2013October 26, 2015

— Valid NPI or Prov# – Start SELECT (last_name + ‘, ‘ + first_name) AS Provider ,LTRIM(RTRIM(NPI_nbr)) AS NPI# ,LTRIM(RTRIM(amisys_prov_nbr)) AS Provider# ,( SELECT COUNT(*) TotalCount FROM MedicareHealthAssessment WHERE providerid = LTRIM(RTRIM(vp.amisys_prov_nbr)) GROUP BY providerid ) AS TotalCountByProvNum ,( SELECT COUNT(*) TotalCount FROM MedicareHealthAssessment WHERE prov_NPI = LTRIM(RTRIM(vp.NPI_nbr)) GROUP BY…

Read More

Show Duplicate Records

Posted on February 28, 2013October 26, 2015

These two queries run under different schools of thought and purpose but will help in finding duplicate records. First, if you only want to see the records that are duplicates and the amount of times they appear in the table you could run this code: SELECT memberid ,COUNT(*) AS No_Of_Occurrences…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes