Forum Moderators: coopster

Message Too Old, No Replies

Export MySql to CSV

how do I export the fields within a table

         

stampjr

8:16 pm on Nov 12, 2007 (gmt 0)

10+ Year Member



I am quite the noob at php, but I have a simple script that downloads my sql tables to CSV (opens nicely in Excel)... The problem is I have a param called 'app_values'... it contains info from an application. I need to get each of those into a column.

<?php
$db = mysql_connect('localhost', 'USER', 'PWD); // Connect to the database
$link = mysql_select_db('DB_NAME', $db); // Select the database name

function parseCSVComments($comments) {
$comments = str_replace('"', '""', $comments); // First off escape all " and make them ""
if(eregi(",", $comments) or eregi("\n", $comments)) { // Check if I have any commas or new lines
return '"'.$comments.'"'; // If I have new lines or commas escape them
} else {
return $comments; // If no new lines or commas just return the value
}
}

$sql = mysql_query("SELECT date, app_values FROM jos_applicants ORDER BY date") ; // Start our query of the database
$numberFields = mysql_num_fields($sql); // Find out how many fields we are fetching

if($numberFields) { // Check if we need to output anything
for($i=0; $i<$numberFields; $i++) {
$head[] = mysql_field_name($sql, $i); // Create the headers for each column, this is the field name in the database
}
$headers = join(',', $head)."\n"; // Make our first row in the CSV

while($info = mysql_fetch_object($sql)) {
foreach($head as $fieldName) { // Loop through the array of headers as we fetch the data
$row[] = parseCSVComments($info->$fieldName);
} // End loop
$data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row
$row = ''; // Clear the contents of the $row variable to start a new row
}
// Start our output of the CSV
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=Buckingham_Applicants.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $headers.$data;
} else {
// Nothing needed to be output. Put an error message here or something.
echo 'No data available for this CSV.';
}
?>

Here is an example of the fields inside as shown in Excel:
a:80:{s:8:\""position\"";s:10:\""Management\"";s:4:\""date\"";

I need s:8 result to show in a column of its own.

Please help. and thank you in advance

eelixduppy

5:43 am on Nov 13, 2007 (gmt 0)



Do you have phpmyadmin installed on your server? If so, there is a nice export feature that allows you to export a database into a CSV file.

stampjr

9:41 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



I have PhpMyAdmin for sure... but I am allowing clients to download lists throughout the site...

This was sent to me earlier, but I am not sure how to implement it.

while ($row = mysql_fetch_object($result)) {
$app_values = unserialize($result->app_values);
// $app_values is now an array
// The rest of your code goes here...
}

So I am in the same spot until I can show someone my whole file.

THX