MySQL Export to csv

Status
Not open for further replies.

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Hi all

I'm trying to code a data export function but having one simple problem.

mysql_connect and select db has been done in seperate connection file

First off, I've created a recordset called $RScontacts.

PHP:
$out = ''; 

// Get all fields names from table.
$fields = mysql_list_fields($database_freecrm,CONTACTS);

// Count the table fields and put the value into $columns. 
$columns = mysql_num_fields($fields);


// Put the name of all fields to $out. 
for ($i = 0; $i < $columns; $i++) {
$l=mysql_field_name($fields, $i);
$out .= '"'.$l.'",';
}
$out .="n";

// Add all values in the table to $out. 
while ($l = mysql_fetch_array($RScontacts)) {
for ($i = 0; $i < $columns; $i++) {
$out .='"'.$l["$i"].'",';
}
$out .="n";
}

// Open file export.csv.
$f = fopen ('export.csv','w');

// Put all values from $out to export.csv. 
fputs($f, $out);
fclose($f);

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="Contact_Export.csv"');
readfile('export.csv');

It works great, BUT - each new row isn't being defined in the csv file so I get one long row with all table row data. - no new lines.


Am I going about this the wrong way?
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Is this supposed to be a newline character?

Code:
$out .="n";
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Is this supposed to be a newline character?

Code:
$out .="n";

Assuming it is, \n = newline, n = just the letter, which may very well be the issue.
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
just a reminder, mysql_list_fields() is deprecated. Which means that it is now only kept for background compability, and for lazy coders. You should be better off switching to mysql_query("SHOW COLUMNS FROM sometable").
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Is this supposed to be a newline character?

Code:
$out .="n";

Oops!
Edit:
just a reminder, mysql_list_fields() is deprecated. Which means that it is now only kept for background compability, and for lazy coders. You should be better off switching to mysql_query("SHOW COLUMNS FROM sometable").

I have to admit, SQL queries are not my forte so I wasn't aware of this, but I will try this later this evening.
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I have to admit, SQL queries are not my forte so I wasn't aware of this, but I will try this later this evening.
I found out about the deprecation when I looked it up. I've never used the mysql_list_fields functions. I always use SQL...
 

tomasonal

New Member
Messages
5
Reaction score
0
Points
0
Your issue looks interesting. There is a component specializes in large amount of data export, and many formats are supported, such as Excel,Word,PDF and so on. If you want to export data from database, you may choose it, it's very fast and stable when you export data from database. And now, it's free for everyone. You may visit here for further information: http://www.codeproject.com/KB/cs/Excel_PDF_Word_ExportWiz.aspx?msg=3767223#xx3767223xx And if you have questions, you can send email to me, my email address is: tomlutao@gmail.com
 
Status
Not open for further replies.
Top