Solved

Export MYSQL to .CSV Add Column Names PHP

Posted on 2016-11-08
2
66 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
[X]
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
2 Comments
 
LVL 22

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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