Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Export MYSQL to .CSV Add Column Names PHP

Posted on 2016-11-08
2
46 Views
Last Modified: 2016-11-08
Here is my code to export my MySQL table to .csv. How do I add the column names to it ? Thanks

<?php
    // mysql database connection details
    $host = "localhost";
    $username = "";
    $password = "";
    $dbname = "";

    // open connection to mysql database
    $connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
    
    // fetch mysql table rows
    $sql = "select * from masterbrewlist";
    $result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));

    $fp = fopen('masterlist.csv', 'w');

    while($row = mysqli_fetch_assoc($result))
    {
        fputcsv($fp, $row);
    }
    
    fclose($fp);

    //close the db connection
    mysqli_close($connection);
?>

Open in new window

0
Comment
Question by:Ray Zuchowski
2 Comments
 
LVL 21

Accepted Solution

by:
JesterToo earned 500 total points
ID: 41879255
Notice I've given some values to $username, $password, and $dbname.

<?php
    // mysql database connection details
    $host = "localhost";
    $username = "lynn";
    $password = "gauge";
    $dbname = "alphacrm";
    $first = true;

    // open connection to mysql database
    $connection = mysqli_connect($host, $username, $password, $dbname)
       or die("Connection Error " . mysqli_error($connection));

    // fetch mysql table rows
    $sql = "select * from tcompany";
    $result = mysqli_query($connection, $sql)
       or die("Selection Error " . mysqli_error($connection));

    $fp = fopen('masterlist.csv', 'w');

    while($row = mysqli_fetch_assoc($result))
    {
        if ($first) {
           $first = false;
           fputcsv($fp, array_keys($row));
        }
        fputcsv($fp, $row);
    }

    fclose($fp);

    //close the db connection
    mysqli_close($connection);
?>

Open in new window

1
 

Author Closing Comment

by:Ray Zuchowski
ID: 41879301
Excellent. Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question