Forum Moderators: coopster

Message Too Old, No Replies

fighting a bad case of MySQL to CSV

As per jatar_K's script

         

henry0

6:14 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I neeed to trigger an "open or save"
and get content organized as an XL or CSV

but it only outputs in a browser and all in a block without col, rows and col names.
<<<
<?
@ob_start();
include "../root_db_conn/db_fns.php";

// error_reporting (E_ALL);

$conn=db_connect();

$result = mysql_query($sql);

$content_file = "";

$content_file .= "id,username,url,url_title,url_intro,published\r\n";
$sql = "SELECT * FROM suggest_url";
$statement = mysql_query($sql);
while($row = mysql_fetch_array($statement)) {
$content_file .= $row['id'] . ',' . $row['username'] . ',' . $row['url'] . ',' . $row['url_title'] . ',' . $row['url_intro'] . ',' . $row['published']. "\r\n";
}
$output_file = 'url.csv';
@ob_end_clean();
@ini_set('zlib.output_compression', 'Off');
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: none');
//This should work for IE & Opera

header('Content-Type: application/octetstream; name="' . $output_file . '"');
//This should work for the rest
header('Content-Type: application/octet-stream; name="' . $output_file . '"');
header('Content-Disposition: inline; filename="' . $output_file . '"');
echo $content_file;
exit();
?>

>>>>

coopster

6:19 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think that
Content-Disposition:
needs to be
attachment
for starters ;)

henry0

6:28 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm, and I did review it about a zillion times :)

Strange how it did not cranked in my mind

Thanks

case closed

coopster

6:29 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Some times it just takes that extra set of eyes. You're welcome.

henry0

6:46 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry again a small? problem.

The script interprets “, “ as creation of new unwanted col
in one field I have a text excerpt that is anything from 150 to 300 chars long and has here and there some “ , “
Is there a way to address that problem?
Or should I do a preg_replace and insert a double space or another character?

jatar_k

7:02 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use a delimiter for your fields ' or " then the enclosed commas won't cause a problem

henry0

7:10 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I was not clear
the comma are not enclosed in between quotes
I typed the quotes so to meant that there are commas in my fields and not a comma in my thread

jatar_k

7:11 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I know ;)

add quotes around your field data in this line

$content_file .= $row['id'] . ',' . $row['username'] . ',' . $row['url'] . ',' . $row['url_title'] . ',' . $row['url_intro'] . ',' . $row['published']. "\r\n";

then the commas in filed data won't push a new column

henry0

7:57 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's got to be a combo of Friday 13th
and me being quite thick
here is the culprit

I only create errors
Please, show me the way

. $row['url_intro'] .

jatar_k

8:59 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$content_file .= '"' . $row['id'] . '","' . $row['username'] . '","' . $row['url'] . '","' . $row['url_title'] . '","' . $row['url_intro'] . '","' . $row['published']. "\"\r\n";

henry0

9:25 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The day I will not learn something will be the day I'll be gone in the ethernet! Thanks

coopster

10:22 pm on Oct 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I didn't realize it was Friday the 13th!