Log in

View Full Version : Export To CSV



tomyknoker
08-20-2007, 03:43 PM
Yes another csv question, I have found many topics but not what I need, I only want to export 4 columns from my database, and also specify column headings for the column names... Is that possible?

Twey
08-20-2007, 04:18 PM
*wince* CSV? Why?

CSV is all very well and good until you have one of your seperator characters in your data. Then you have to implement all sorts of things to get it working. You'd be better off using, say, XML. PHP has a standard config file format, too, which it can read in natively; see parse_ini_file (http://www.php.net/parse-ini-file)().

JShor
08-20-2007, 05:37 PM
What you should do is use mysql_fetch_array to select the rows you want, and then format it so that it looks like a CSV format. From there, cut and paste it into Notepad and save it as .csv format.

Example:


$result = mysql_fetch_array( $result2 );
echo "$result2[row1] , $result2[row2]"



Would look like this:

info1 , info2

Good luck

Twey
08-20-2007, 05:50 PM
Actually you need braces.
echo "{$result2['row1']} , {$result2['row2']}";If you're doing that, though, you might as well:
function mysql_fetch_all($rs) {
$arr = array();
while($row = mysql_fetch_array($rs))
array_push($arr, $row);
return $arr;
}

function mysql_to_csv($rs) {
return implode("\n", array_map(create_function('$a', 'return implode(\',\', $a);'), mysql_fetch_all($rs)));
}

tomyknoker
08-21-2007, 02:58 AM
Thanks guys... Well I got it to run, but it doesn't save anything... This is what I have... Should I be using fputcsv instead? Oh and the reason it's a csv is so it can be imported into a simple gateway app on the web and they only take .csv unfortunately...


$query = "";

$result = mysql_query($query);
$num = mysql_num_rows($result);

function mysql_fetch_all($result) {
$arr = array();
while($row = mysql_fetch_array($result))
array_push($arr, $row);
return $arr;
}

function mysql_to_csv($result) {
return implode("\n", array_map(create_function('$a', 'return implode(\',\', $a);'), mysql_fetch_all($result)));
}

include 'library/closedb.php';
?>

Twey
08-21-2007, 07:53 AM
it doesn't save anything...Why do you expect it to? Surprisingly enough, the mysql_to_csv() function converts a MySQL result resource to CSV. You haven't actually done anything with that data -- in fact, you haven't even called the function.

tomyknoker
08-21-2007, 10:57 AM
But when the page runs isn't it called? I don't understand...

Twey
08-21-2007, 11:11 AM
No... since I don't know what you want to do with the data, I provided two functions (" (http://en.wikipedia.org/wiki/Subroutine)a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code"), the former of which is used by the latter to perform the conversion you required.

tomyknoker
08-21-2007, 11:20 AM
Um sorry that totally confuses me... What I want to do is pull out from my table into a csv file 5 columns, FirstName, LastName, Email, Mobile... I then want to add to the top of the .csv seperate headings... So basically I want to achieve this...

Fname, Sname, Email, Mobile
Tom, Smith, tom@smith.com, 0410000000
James, Smith, james@smith.com, 0410000000

I have tried every which way and can't get a result... I'm not sure where to go from this point... How do I write to the actual .csv file? I mean what do I need to add to the above code to get a file?

tomyknoker
08-21-2007, 12:55 PM
Hiya guys ok worked it all out... This appears to be working correctly, does it look ok?


$result = mysql_query("");

while($row = mysql_fetch_array($result)) {

$textString = $row['MobileNumber'];
$trimmedMobile = preg_replace('/[^\d+]/', '', $MobileNumber);

$csv_output .= "$row[FirstName],$row[LastName],$trimmedMobile,$row[Email]\n";

}

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=" .$date.".xls");
print $csv_output;
exit;

?>