Jump to content

Out put file delimiter(s)


ajf

Recommended Posts

when I am creeating a .csv output file after running my SQL script,

 

I need to have the first part of the information with a comma (,) delimiter and the second half of my data to have another delimiter (e.g. colon(:) )

 

any ideas how i would go about this?

 

thanks

Link to comment
Share on other sites

  • 2 weeks later...

I use the following script to dump report files in both xls and csv, and I found the basic script on the internet somewhere then modified it to suit my purposes. In the example below I have it set to export as a xls so I can open it in Office and save as an xlsx file, but you can simply edit what you need and change the output in the header line to .csv.

$link = mysql_connect ($Host, $User, $Password) or die('Could not connect: ' . mysql_error());mysql_select_db($DBName) or die('Could not select database');$select = "SELECT s.ser_num, m.id, m.class, m.mfg, m.model, m.cpu_type, m.cpu_speed, m.mem, m.optical, m.hdd_size, m.grade, m.sold_to, m.pid, m.inv_num, m.sale_date FROM `".$TableName."`AS s INNER JOIN `maininv` AS m ON s.ser_num=m.ser_num ORDER BY class, mfg, model ASC";$export = mysql_query($select); $fields = mysql_num_fields($export); for ($i = 0; $i < $fields; $i++) {	$csv_output .= mysql_field_name($export, $i) . "t";}while($row = mysql_fetch_row($export)) {	$line = '';	foreach($row as $value) {		if ((!isset($value)) OR ($value == "")) {			$value = "t"; 		} else {			$value = str_replace('"', '""', $value);			$value = '"' . $value . '"' . "t"; 		}		$line .= $value;	}	$data .= trim($line)."n";}$data = str_replace("r","",$data);header("Content-Type: application/vnd.ms-excel");header("Content-Disposition: attachment; filename=sale_report.xls");header("Pragma: no-cache");header("Expires: 0");print $csv_output."n".$data;exit;?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>	<title>Download MySQL Table Code</title></head><body></body></html>
Edited by Count_Zero
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...