Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:03:30+00:00 2026-06-02T10:03:30+00:00

I have a small problem with exporting MySQL data to CSV file, it print

  • 0

I have a small problem with exporting MySQL data to CSV file, it print out the data correctly from the table but at the end of the csv file it prints also the html page code!

Below is my code

$file="billing";
     $i=0;
     $values = mysql_query("SELECT * FROM billing");
     $rown=0;
     while( $row = mysql_fetch_assoc($values)){
     if($rown++==0)
       $csv_output.=implode(";",array_keys($row))."\n";
       $csv_output.=implode(";",array_values($row))."\n";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: text/csv");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
}

Output:

billing_id;customer_id;user_id;trans_id;transfer;balance;created;text;credit;sender_id      
257;29;;-1;0;500000;1330930434;Payment;500000;      
258;29;;-1;200000;300000;1330930465;Sender ID reg.;0;jkjjhh     
284;32;;-1;0;1000000;1331708884;Payment;1000000;        
285;32;564;268;-120;999880;1331709106;SMS send;;        
286;32;;-1;0;1000000;1331709234;Payment;120;        
287;32;564;269;0;1000000;1331723634;SMS send;;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">       
    <html xmlns="http://www.w3.org/1999/xhtml">     
    <head>      
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />       
    <title>Client Manger</title>        
    <link rel="stylesheet" href="/style/my-style.css">      
    <form action='./' method='post'>        
    <input type="hidden" name="action" value="billing" class="text">        
    <input type="hidden" name="customer_id" value=""  />

What could be the reason?

Thanks for all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:03:31+00:00Added an answer on June 2, 2026 at 10:03 am

    You need to have exit; or die(); after the print $csv_output; Otherwise the rest of the page (outside of that close } (which I suspect is the end of an if statement)) will print (and therefore be included in the CSV).


    Just a thought: it might be a better idea to have two PHP files to handle this: one which outputs the CSV, the other which outputs the form. Not only allow you to avoid using die/exit or very long else statements it will also result in cleaner code overall.


    As another thought, you are better off using do...while any time you want to have something happen the first time (and only the first time). So this might be a better option:

    $row = mysql_fetch_assoc($values);
    $csv_output.=implode(";",array_keys($row))."\n";
    do
    {
        // notice how there is no flag needed!
        // also, you don't need to call 'array_values' when imploding
        $csv_output.=implode(";",$row)."\n";
    }while( $row = mysql_fetch_assoc($values));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am generating new xml file using perl script but I have small problem,
I would like to write to a text file, but I have small problem.
I have small problem. I'm trying to work on an XML file, but the
I have small problem. when i include file without ob_start, head's codes comes in
i have small problem i.e. their is one celltable with lot of data it
I have small problem and it might be silly somewhere, but still i have
I have small problem with JavaMail. Sometimes method getRecipients from class Message returns weird
I am using Jquery Autocomplete . Its workng fine but I have small problem.
I have a small problem but couldn't find solution for that. I have founded
have small problem, and would very much appreciate help :) I should convert byte

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.