Jump to content

Changing field name when MySQL information is dumped into a CSV


skateme

Recommended Posts

Hi,

 

I have a script that dumps the information from one of my MySQL tables into a CSV. The script works great, but in the result, the names of the MySQL columns are shown as-is. In other words, I have a column named "field4" that shows up as "field4" in the CSV. How can I modify the columns to more descriptive names without modifying the database itself?

 

Here's my code:

<?php
$host = "*********";
$user = "*********";
$pass = "*********";
$db = "database_name";
$table = "table";
$file = "member_information";

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

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;

if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");

while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}

$filename = $file."_".date("m.d.y");

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("m.d.y") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");

print $csv_output;

exit;
?>

 

The CSV it spits out looks like this:

field1    field2     field3    field 4
info      info        info       info

 

I want it to look like this:

Name      Email      Location      Age
info         info          info             info

 

Thanks in advance!

Link to comment
Share on other sites

Hi thanks for the reply. I think option B is best, however the output still appears the same. My updated code:

 

<?php
$host = "*********";
$user = "*********";
$pass = "*********";
$db = "database_name";
$table = "table";
$file = "member_information";

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

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;

if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;}
}
$csv_output .= "\n";
$values = mysql_query("SELECT userid AS \"UserID\",temp as \"\",field1 AS \"Name\",field2 AS \"HighSchool\",field3 AS \"HSGraduation\",field4 AS \"College\",field5 AS \"CollegeGraduation\",field6 AS \"Major\", field7 AS \"InternshipSite\",field8 AS \"Email\" FROM ".$table."");

while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}

$filename = $file."_".date("m.d.y");

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("m.d.y") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");

print $csv_output;

exit;
?>

 

Any advice? Thanks!

Link to comment
Share on other sites

The alias names only exist in the result for the query that contains the alias names. You could use mysql_field_name to get those names after you execute that query (there's no need for a separate SHOW COLUMNS ... query) -

 

<?php

$values = mysql_query( ... ... );

$i = 0;
while ($i < mysql_num_fields($values)) {
$names[] = mysql_field_name($values,$i);
$i++;
}
$csv_output = implode(',',$names) . "\n"; // implode the names

// your while(){} loop to retrieve the data from the query would go here...
while( ... ... ){

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.