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

x
?
Solved

Insert new column into CSV export using PHP and MySQL

Posted on 2011-11-01
2
Medium Priority
?
363 Views
Last Modified: 2012-05-12
I am using the following code to generate a .csv file based on a MySQL query using PHP.  This works great except for I have to add a column called Country that is not found in the table.  How do I update this code to insert a column called Country for each record and insert USA as the data when inserting the data into the .csv?

<?php
$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";
  }
  $row++;

  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;
exit;
?>

Open in new window

0
Comment
Question by:pda4me
2 Comments
 
LVL 4

Accepted Solution

by:
mars-vie earned 2000 total points
ID: 37063603
Try this -->

<?php
$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 .= "Country". "; ";
    $csv_output .= "\n";
  }
  $row++;

  foreach($rowr as $name => $value)
  {
    $csv_output .= $value . "; ";
  }
  $csv_output .= "USA". "; ";
  $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;
exit;
?>


M@RS
0
 

Author Closing Comment

by:pda4me
ID: 37063757
Perfect, thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

867 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