Re: Exporting From MySQL to .csv using PHP



mpar612 <mpar612@xxxxxxxxx> wrote:

Hello,

I'm sort of new to PHP. I am using the following code to retrieve a
column from a database and to put all of the results into a .csv
file. This allows creates a .csv file, but only lists the first
result from the database. It is not returning all of the rows. I
know the issue is somewhere in my "foreach" but I'm not sure where.

Any help would be great. Thanks!

<?php

$selectSql = "SELECT artist_name FROM lounge_products";
$selects = $db->getAll($selectSql);

foreach($selects as $select) {

$content = $select[artist_name].",";

Concat:
$content .= $select[artist_name].",";

In the current code you keep overwriting $concat with the new value.
If you db object returns an array, this would be simpler:

$content = implode(',',$selects);

Now, all this code will not help you if an artists name contains a comma, all your logic is lost.

For creating CSV's, I'd use this:
header("Content-Disposition: attachment; filename=export.csv");
$selectSql = "SELECT artist_name FROM lounge_products";
$selects = $db->getAll($selectSql);
$out = fopen('php://output','w');
foreach($selects as $select) fputcsv($out,$select);



Alternatively, use mysql's built in option to export a file, which would also take care of escaping:
$file = '/path/to/file';
mysql_query("SELECT artist_name
INTO OUTFILE '{$file}'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
FROM lounge_products");
header("Content-Disposition: attachment; filename=export.csv");
readfile($file);
--
Rik Wasmus
Posted on Usenet, not any forum you might see this in.
Ask Smart Questions: http://tinyurl.com/anel
.



Relevant Pages

  • Re: PHP/MySQL project
    ... > Looking to build a dynamic site using PHP/MySQL. ... > import changes to the database easily. ... Import the csv file into the MySql db, and then use phpmyadmin in the future ... The version of PHP will be important if you are considering finding / buying ...
    (alt.php)
  • Re: CSV(???)
    ... What csvline does is straightforward: ... into a csv file, the idea being that the office ... to a real database? ... The boss wanted me to use php because Python ...
    (comp.lang.python)
  • Re: php in database entry...
    ... in my main php file i retrieve the field and i echo it (echo ... now i need in this field to have a php script to retrieve some info from another field, so lets say that the content ... it's a very bad idea to keep PHP code in a database. ...
    (comp.lang.php)
  • Re: Saving a form to the server
    ... In a directory by the guides name which would be the variable ... When you submit a form to a PHP page, that PHP script can access the ... You should probably save it into a database. ... The data is easier to store than retrieve with flat files. ...
    (comp.lang.php)
  • Re: HELP! Urgent. Using JS to retrieve data from DB = mission impossible?
    ... Javascript with Informix as the back-end. ... Is it possible to retrieve data using Javascript but by accessing the ... You may still need to use PHP or some other intermediary, ... are prepared to open your database to remote access. ...
    (comp.lang.javascript)