0
while($row = mysql_fetch_array($result)) {


                echo "<tr>";
                echo '<td>' . $row['fname'] . '</td>';
                echo '<td>' . $row['lname'] . '</td>';

                echo '<td>' .(( $row['gender']=='m' )?'male':'female'). '</td>';


                echo '<td>' . $row['dob'] . '</td>';
                echo '<td>' . $row['mobnum'] . '</td>';
               // echo '<td>' . $row['mobnum'] . '</td>';
                echo '<td>' . (($row['selqual']=='s')?'school':'college'). '</td>';

                echo '<td>' . $row['address'] . '</td>';
                echo '<td>' . $row['email_id'] . '</td>';

                //get course = calling function
               echo '<td>' . getcourse($row['course_name']) . '</td>';
               echo '<td>' . $row['date_time'] . '</td>';

             // echo '<td>'. $course_name=(explode(',',$_row['course_name'])) . '</td>';



                echo "</tr>"; 

   }

        echo "</table>";

        }

Hi i am fresh to php. I am populating and displaying these results in while loop from various tables in a db. I want to export those data to csv format. is there any way i can store them in an array and use the array for exporting??. if yes please guide me.

  • Refer this http://stackoverflow.com/questions/356578/how-to-output-mysql-query-results-in-csv-format – guy_fawkes Mar 03 '14 at 13:07
  • You say you're new to PHP: in which case, please don't waste your time learning about the _deprecated_ `mysql_*` extension, use `mysqli_*` or `PDO` instead... – Elias Van Ootegem Mar 03 '14 at 13:13

3 Answers3

0

Exporting to csv is so common PHP even has native functions available. Look at fputcsv.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

Add header On the top of page.

 header("Content-type: text/csv");
 header("Content-Disposition: attachment; filename=file.csv");
 header("Pragma: no-cache");
 header("Expires: 0");

Hope this help. Thanks

  • Dear lord, a copy paste using `$obj_jobs`, an instance of an unknown class, that _doesn't_ produce a CSV file?? how could this help the OP? – Elias Van Ootegem Mar 03 '14 at 13:19
0

Using PHP, it's really easy to write the data to a CSV file. The things you need to do are:

  • Read the red warning boxes on the site
  • don't fetch using mysql_fetch_array (not talking about the deprecation of the extension) this fetches the results twice, once numerically indexed, and once as an assoc array. Fetch as assoc array only: mysql_fetch_assoc
  • Open a file: fopen('filename.csv', 'w'); should do fine
  • write to the file using fputcsv

Anyway:

$csv = fopen('out.csv', 'w');
if (!$csv)
{
    exit('Failed to open csv');
}
while($row = mysql_fetch_assoc($result))
{
    fputcsv($csv, $row);//writes line to CSV
    //process $row further, if you want
}
fclose($csv);//<-- don't forget to close the file

And if, for some reason, you want to have an array containing all the data before writing that same data to a file:

$data = array();//<-- big array
while($row = mysql_fetch_assoc($result))
{
    $data[] = $row;//add row to data array
}
//code
//then:
$fh = fopen('export.csv', 'w');
foreach ($data as $line)
{
    fputcsv($fh, $line);
}
fclose($fh);

Easy.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Thank you so much for your clear explanation.. I will follow as you said. – user3354698 Mar 03 '14 at 13:24
  • @user3354698: You're welcome, in that case, would you be so kind as to [accept my answer](http://stackoverflow.com/help/someone-answers). The help section of this site asks _not_ to post _"Thank you"_ comments, but to mark the answer in question as accepted instead – Elias Van Ootegem Mar 03 '14 at 13:34