Putting field names in a CSV

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Putting field names in a CSV

Post by michaelk46 »

I have been able to export the data from a MySQL database table and place it into A csv file, but I am not sure how to get the table field names to put them into the file first. The only command I have found that specifically did that is deprecated. Can someone give me a nudge in the right direction on which command I can use or maybe some resources that someone found to fix this issue.

Thanks All
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Putting field names in a CSV

Post by AbraCadaver »

There are examples on the mysql_list_fields() page that use a "SHOW COLUMNS FROM sometable" appraoch, but I would try this in your loop:

Code: Select all

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if(!isset($field_names)) {
        $field_names = array_keys($row);
    }
    // whatever you do with the $row array
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: Putting field names in a CSV

Post by michaelk46 »

Thanks dude...
Post Reply