PHP Export CSV alter column titles from MySQL query

Posted on 2011-11-01
Last Modified: 2012-05-12
I am currently using the following script and it is creating a CSV file based on a MySQL query.  The column titles are the column names in the table from the query.  I want to be able to specify what I want the column titles to be (my_column_name1, etc.) instead.  How do I alter this code to do this?

$host = 'localhost';
$user = 'mysqlUser';
$pass = 'myUserPass';
$db = 'myDatabase';
$table = 'products_info';
$file = 'export';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$values = mysql_query("SELECT COLUMN1, COLUMN2, COLUMN3 FROM ".$table."");
$row = 0;
while ($rowr = mysql_fetch_assoc($values)) 
  if ($row == 0)
    foreach($rowr as $name => $value)
      $csv_output .= $name . "; ";
    $csv_output .= "\n";

  foreach($rowr as $name => $value)
    $csv_output .= $value . "; ";
  $csv_output .= "\n";

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;

Open in new window

Question by:pda4me
    LVL 4

    Accepted Solution

    Is this what you are looking for -->

    $values = mysql_query("SELECT
    COLUMN1 AS New_column_title1,
    COLUMN2 AS New_column_title2,
    COLUMN3  AS New_column_title3
    FROM ".$table."");

    You can also put your wish name in variables.


    Author Closing Comment

    PERFECT, thanks!

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
    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.  …
    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…
    The viewer will learn how to count occurrences of each item in an array.

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now