[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


PHP Export CSV alter column titles from MySQL query

Posted on 2011-11-01
Medium Priority
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/vnd.ms-excel");
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

Accepted Solution

mars-vie earned 2000 total points
ID: 37063233
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

ID: 37063289
PERFECT, thanks!

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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…
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…
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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