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 8230049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:00:20+00:00 2026-06-07T17:00:20+00:00

I have some code that written in php to use for export data from

  • 0

I have some code that written in php to use for export data from mysql to xls, but I have something wrong with the output php.

here’s the code

<?php
// koneksi database
$db = "k8665xxxx";

mysql_connect("localhost","k866xxx","kixxx");
mysql_select_db("$db");

$date = date('d-m-Y');
$filename = "Data-inquiry-downloaded-$date.xls";

// nilai awal counter untuk jumlah data yang sukses dan yang gagal diimport
$sukses = 0;
$gagal = 0;
$table = "promo";


//jumlah kolom
$jkolom=0;

//generate kolom
$q= mysql_query("select * from $table where last_update between '$_POST[DariTanggal]' AND '$_POST[SampaiTanggal]'");
$r=mysql_fetch_assoc($q);
foreach ($r as $head=>$nilai) {
$judul = str_replace("Promo","Inquiry","$head");
$header .=$judul."\t"; //print header table
$jkolom++;
}
$header .= "\n";

//generate baris
$result= mysql_query("select * from $table where last_update between '$_POST[DariTanggal]' AND '$_POST[SampaiTanggal]'");
while ($row=mysql_fetch_array($result)) {
for ($x=0; $x<$jkolom; $x++) {
$content .=$row[$x]."\t";
}
$content .= $row[$x]."\n";
}


$output .= $header.$content;
header('Content-type:application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;

?>

And here’s the output…

622 Joni    bekasi  2193850955      4/11/2012                   spooring        yani    TELP        Bekasi  deal
623 rengko      81383691374 cek distributor komo    4/11/2012                   33x12.5x15  ban rosi    TELP        DKI Jakarta 
624 Joko Prayitno               4/11/2012                   175/65/14   ban rosi    EMAIL   jokfis2001@yahoo.com    DKI Jakarta 
625 diana   Jl.landas pacu timur blok D No.5kemayoran   818772885       4/11/2012   B1603BFF    Honda   2009    jazz Rs ban 185/55/R16  ban yani    YM  diana.william@ymail.com Jakarta Pusat   deal
626 Indra   Jln.Terusan I Gusti NgurahRay Pondok Kopi                                                       
    8129496491      4/11/2012   B8723KF Suzuki  2004    baleno  paket dasi  paket dasi  rosi    TELP    ind_dra@yahoo.com   Jakarta Timur   deal        
627 ari depok   2128639500  mau konfirmasi lagi     4/11/2012                   195/60/15   ban rosi    TELP        Depok   

Let me get the details, look at the number 626.. the data is like this

626 Indra   Jln.Terusan I Gusti NgurahRay Pondok Kopi                                                       
    8129496491      4/11/2012   B8723KF Suzuki  2004    baleno  paket dasi  paket dasi  rosi    TELP    ind_dra@yahoo.com   Jakarta Timur   deal        

But, it must be…

626 Indra   Jln.Terusan I Gusti NgurahRay Pondok Kopi  8129496491      4/11/2012   B8723KF Suzuki  2004    baleno  paket dasi  paket dasi  rosi    TELP    ind_dra@yahoo.com   Jakarta Timur   deal 

And lot of my data become like that.. anybody have any idea about my problem?

  • 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-07T17:00:22+00:00Added an answer on June 7, 2026 at 5:00 pm

    I guess you have a rowbreak in the text in your database,
    at the 2 rows like $content .=$row[$x]."\t";, use str_replace() to remove \n, or use php:s built in csv functions (like Phil) suggested.

    If you want to build more complex xls, not just data in csv, you may want to read about the “Excel XML 2003”-files

    $content .= str_replace("\n", " ", $row[$x]) . "\t";
    

    Update

    or just trim away all unnecessary whitespace

    $content .= trim($row[$x]) . "\t";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that has been written in for php 5.3.0 using the
I have written some code that loads an XML document using an XmlDocument object
I have written some code to ensure that items on an order are all
I have a c++ source code that was written in linux/unix environment by some
I have written some WebGL code, actually I am playing with the examples that
I have an application written in C# that invokes some C code as well.
I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
I have written a php code which will get one id from database and
I have inherited some legacy PHP code what was written back when it was
I am getting varied results with some PHP code I have written to upload

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.