Jump to content

[SOLVED] Getting CSV Column Names


PHPNewbie55

Recommended Posts

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..??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.