PHPNewbie55 Posted December 19, 2007 Share Posted December 19, 2007 I am trying to create a CSV Import Script.. I can import and get the data into MySQL... no problem... But I have to define everything in the script beforehand What I am trying to do now is make it so that I can MAP any CSV file to my MySQL Database.. What I am having a problem with is getting the first row of the CSV file to get the NAMES of each CSV Column.. Here is how I am getting the MySQL Column Names:::: <?php $get_columns = mysql_query("SELECT * FROM $mysqltbl"); while ($columns = mysql_fetch_field($get_columns)) { $mysql_column_names .= "".$columns->name."<br>"; ?> That works... But I can't seem to figure out how to do the same thing with the CSV file.... <?php $CSV_File = "Test_data.csv"; $csv = fopen($CSV_File, 'r'); while ($data = fgetcsv ($csv, 10000, ",")) { $csv_column_names .= "".$data->name."<br>"; } ?> Any ideas how to just grab the column names in the csv file..?? Quote Link to comment Share on other sites More sharing options...
chigley Posted December 19, 2007 Share Posted December 19, 2007 <?php $CSV_File = "Test_data.csv"; $csv = file($CSV_File); $columns = explode(",", $csv[0]); $csv_column_names = implode(" ", $columns); ?> Give that a whirl Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 19, 2007 Author Share Posted December 19, 2007 That works perfectly... Thank You! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.