export data to csv format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • farukcse
    New Member
    • Nov 2007
    • 52

    export data to csv format

    Dear Sir,
    Can anyone tell me how to export data from MySQL database to a csv file using php code i.e without using phpmyadmin?

    Regards,
    Fauk Chowdhury.
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Checkout this small post.

    Comment

    • farukcse
      New Member
      • Nov 2007
      • 52

      #3
      Originally posted by mwasif
      Checkout this small post.
      Dear Sir,
      Thanx for your reply.i modified your code according to my requirement but i am facing a problem with characters set i.e there are some data at database like as plate_no : 1 2 3 ص ص ص but when i exported it in csv format i see it as 1 2 2 3 س س س. so can you tell me where is my problem or how can i solve it.
      [php code]
      <?php
      header('Content-Type: text/html; charset=utf-8');
      // Connect to database
      mysql_connect(" localhost", "root", "root123");
      mysql_select_db ("medgulf");

      // mention the file name to display in download prompt
      $file_name = "download.c sv";

      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"$fil e_name\"");
      header("Cache-Control: cache, must-revalidate");
      header("Pragma: public");

      // Output header
      //echo "Name,Code"."\n ";

      $result = mysql_query("SE LECT * FROM vehicle_info");
      while($rs = mysql_fetch_arr ay($result))
      {
      echo $rs["user_name"].",";
      echo $rs["entry_date "].",";
      echo $rs["id_no"].",";
      echo $rs["license_ty pe"].",";
      echo $rs["vehicle_ty pe"].",";
      echo $rs["plate_no"].",";
      echo $rs["chassis_no "].",";
      echo $rs["body_type"].",";
      echo $rs["model_year "].",";
      echo $rs["status"]."\n";
      }
      ?>

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Are you viewing the csv in a spreadsheet program or in a plain text program?
        i think this is the culprit rather than your code.

        Comment

        • farukcse
          New Member
          • Nov 2007
          • 52

          #5
          Dear Sir,

          i am veiwing this in spreadsheet . is spreadsheet making problem ? how to solve this? please reply .

          Thanks inadvance,

          faruk chowdhury

          Comment

          • farukcse
            New Member
            • Nov 2007
            • 52

            #6
            Dear Sir,

            i am veiwing this in spreadsheet . is spreadsheet making problem ? how to solve this? if i want ot make a text file what i have to do? please reply .

            Thanks inadvance,

            faruk chowdhury

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              I am not sure of how much control can be weilded to force another application on how to interpret a character set.
              Assuming you are using Excel, Excel is actually converting the characters.
              Do they look OK in notepad.
              If so the data is fine, it is Excel causing the problem

              Comment

              • farukcse
                New Member
                • Nov 2007
                • 52

                #8
                Dear Sir,
                Excel is causing the problem.if i save it in .txt format then there is no problem. but i need to save it in excel fomat . so can you tell how can i do it?

                Regards,
                Fauk Chowdhury.

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  Excel is causing the problem.if i save it in .txt format then there is no problem. but i need to save it in excel fomat
                  No, you need to OPEN it in Excel format, you are saving it in CSV format.
                  Excel is interpretating your character set wrongly when you open the csv file.
                  Notepad isn't which proves your data IS NOT being corrupted.
                  I don't know how to tell Excel to transalate any character set a certain way.
                  Not even sure you can.
                  Sorry, all I can suggest is studying the subject of character encoding.
                  I don't know much about it.

                  Comment

                  Working...