Solved

Export MYSQL to .CSV Add Column Names PHP

Posted on 2016-11-08
2
22 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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.  …
The viewer will learn how to count occurrences of each item in an array.
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now