•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,697 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,148 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 826 | Replies: 1
![]() |
•
•
Join Date: Oct 2005
Location: Harare, Zimbabwe
Posts: 33
Reputation:
Rep Power: 3
Solved Threads: 2
i have this code:
[code]
include('DB_connection.php');
$result = mysql_query('select * from login');
$count = mysql_num_fields($result);
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);
# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}
# This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");
# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data;
?>
[quote]
the problem is that it is displaying everything in the first field
[code]
include('DB_connection.php');
$result = mysql_query('select * from login');
$count = mysql_num_fields($result);
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);
# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}
# This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");
# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data;
?>
[quote]
the problem is that it is displaying everything in the first field
•
•
Join Date: Aug 2007
Location: Morrisdale, PA
Posts: 52
Reputation:
Rep Power: 2
Solved Threads: 5
I have tested you code and found that it works.
the only thing I did different was place this code at the top of the script:
once I did that it worked fine. It may not be the best solution, but it did work after I placed that within the code.
the only thing I did different was place this code at the top of the script:
error_reporting(E_ALL ^ E_NOTICE);
once I did that it worked fine. It may not be the best solution, but it did work after I placed that within the code.
I keep hitting "escape", but I'm still here!!!!
:}
:}
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
- Extracting select option fields from mySQL (PHP)
- retrieving a single cell of data from a MySQL database (PHP)
- Problem adding registration data to mysql database (JSP)
- Selecting MYSQL into fields (HTML and CSS)
- MySQL. Setup? (MySQL)
- How Do You Connect MySQL to localhost? (MySQL)
Other Threads in the PHP Forum
- Previous Thread: loopy doopy
- Next Thread: quickly getting from sql


Linear Mode