Export Data From Mysql In Csv

  • 3 years-4 months

    Export Data From Mysql In Csv

    • Category : php
    • Viewed : 68 times
    • Posted On :
    • Posted by : Guley
    <?php 
    include('config.php'); //connection file
    //header to give the order to the browser
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment;filename=user.csv');
    //select table to export the data
    $select_table=mysql_query('select `email` from login');
    $rows = mysql_fetch_assoc($select_table);
    
    if ($rows)
    {
    getcsv(array_keys($rows));
    }
    while($rows)
    {
    getcsv($rows);
    $rows = mysql_fetch_assoc($select_table);
    }
    
    // get total number of fields present in the database
    function getcsv($no_of_field_names)
    {
    $separate = '';
    
    
    // do the action for all field names as field name
    foreach ($no_of_field_names as $field_name)
    {
    /*if (preg_match('/\\r|\\n|,|"/', $field_name))
    {
    $field_name = '' . str_replace('', $field_name) . '';
    }*/
    echo $separate . $field_name;
    
    //sepearte with the comma
    $separate = ',';
    }
    
    //make new row and line
    echo "\r\n";
    }
    ?>

     

Report this or Ask your own question

Your Answer

When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.