?
Solved

Export MYSQL to .CSV Add Column Names PHP

Posted on 2016-11-08
2
Medium Priority
?
71 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 2000 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

771 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