Exporting some MySQL fields to a .CSV file

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Exporting some MySQL fields to a .CSV file

Post by robster »

Hi all,

I've had a good scour through these forums and across google for some how to's and it's starting to confuse me a little so I thought I'd ask at the place where it's at... ;)

As the Subject says, I want to export a bunch of stuff to a .CSV file. To be exact, I want to export the Name and Address of each customer in my MySQL database so I can do a mailmerge in OpenOffice and print out all the envelopes required.

Details of table (and the fields I've chosen to print) are as such:

Code: Select all

Table:  Clients

Chosen Fields:  name_first, name_last, add_street, add_city, add_state, add_pcode
What I want it to do then is roughly this:

Code: Select all

$connection = mysql_connect($dbhost, $dbusername, $dbpassword);

	$dir = "DESC";
	mysql_select_db($dbname);
	$sql = "SELECT * FROM clients ORDER BY name_last ASC";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$cShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$cShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$id = $Xcontent["id"];
		$name_first = $Xcontent["name_first"];				
		$name_last = $Xcontent["name_last"];
		$phone_home = $Xcontent["phone_home"];
		$add_street = $Xcontent["add_street"];
		$add_city = $Xcontent["add_city"];			
		$add_state = $Xcontent["add_state"];
		$add_pcode = $Xcontent["add_pcode"];
		$bday_day = $Xcontent["bday_day"];
		$bday_month = $Xcontent["bday_month"];
		$bday_year = $Xcontent["bday_year"];
		
		
	
		if ($add_street != "") //if they have a street address (for postage)
		{
			//add this data to the csv file
                        name_first, name_last, add_street, add_city, add_state, add_pcode
		}
	$Xcontent = mysql_fetch_array($content);
	//mysql_free_result($content);
	}

Any pointers would really be appreciated. I saw PHP has the ability to read a csv file and perhaps even write one, but it didn't make a lot of sense to me. I'm not the most advanced programmer by any means.

Thanks again, I really appreciate any help,

Rob


(EDIT: I should note that addresses sometimes have / and , and ' in them. An example is this:

Code: Select all

name_first: John
name_last: Smith
add_street: Shop 2/25 Corner of Mc'Farlane, Davidson Street
add_city: Michealton
add_state: Queensland
add_pcode: 4568
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

First skill you might want to improve: searching the web

The first i thought was: "openoffice mysql mailmerge".
The first result google gave me was: http://linux.sys-con.com/read/32634.htm.
Meaby this one is informative too: http://oooauthors.org/en/FAQs/Database/ ... older_view

I'm pretty sure you would end up with pretty good results too if you search for "php cvs writer".
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Thanks so much :)

I never considered a direct leach from the database via ODBC... I'm following this tutorial now: http://ericzander.com/Publications/OO_M ... /index.htm

Looks like I don't need a CSV file at all which is even better.

I'll report back on my findings for anyone who is interested in the future.

Rob
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

I am using the 2.x beta of OpenOFfice and that tutorial was PERFECT! I have access to the actual MySQL database right there in OpenOffice now and can print envelopes/do anything I want via good structured queries applied to forms (as in mailmerge form letters etc).

Thanks again, what a treat!

Rob
Post Reply