473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

using php to export data from mysql to excel

please help. my code displays two errors that I'm having a hard time to resolve. here are the errors:

first is:
missing name in export statement;

it generates an excel file. but this error still pops up for some reason.
(I am able to generate an excel file when I directly put a query string.)

second is:

I'm trying to use a standard code for all the tables that need to be exported to excel. I have one file called "export.php" that my program calls. But this mysql error that says "Query empty", pops up.

Please help. I really need help.
Oct 30 '07 #1
9 5944
dafodil
392 256MB
Please post your code. I convert mysql to excel by passing the values to xml first and then do the exporting.
Oct 30 '07 #2
Please post your code. I convert mysql to excel by passing the values to xml first and then do the exporting.
I know who you are. I know where you study. I also know that you are from a country called the Philippines.

I know your real name and your age.

Watch out.

hahaha. I'm just joking dude. (",)

Back to the topic.

I've managed to export data from mysql to excel without needing to convert the data to xml.

my problem is regarding the error that pops up. the second error popped up when I changed the query option. I changed it so that a query from that page that called "export.php" will be passed.

If you could provide a suggestion as soon as possible. I will be very thankful.
Oct 30 '07 #3
dafodil
392 256MB
You don't have to worry about posting your code. We will not copy your code. We are here to help.
Oct 30 '07 #4
dafodil
392 256MB
Ok here's the code I'm using:


Expand|Select|Wrap|Line Numbers
  1.     $sPrefix = "<html xmlns:x=\'urn:schemas-microsoft-com:office:excel\'>";
  2.     $sPrefix .= "<head>";
  3.     $sPrefix .= "<!--[if gte mso 9]><xml>";
  4.     $sPrefix .= "<x:ExcelWorkbook>";
  5.     $sPrefix .= "<x:ExcelWorksheets>";
  6.     $sPrefix .= "<x:ExcelWorksheet>";
  7.     $sPrefix .= "<x:Name>Department name!!!!</x:Name>";
  8.     $sPrefix .= "<x:WorksheetOptions>";
  9.     $sPrefix .= "<x:Print>";
  10.     $sPrefix .= "<x:ValidPrinterInfo/>";
  11.     $sPrefix .= "</x:Print>";
  12.     $sPrefix .= "</x:WorksheetOptions>";
  13.     $sPrefix .= "</x:ExcelWorksheet>";
  14.     $sPrefix .= "</x:ExcelWorksheets>";
  15.  
  16.     $sPrefix .= "</x:ExcelWorkbook>";
  17.     $sPrefix .= "</xml>";
  18.     $sPrefix .= "<![endif]-->";
  19.     $sPrefix .= "<title>Tracking report</title>";
  20.     $sPrefix .= "</head>";
  21.     $sPrefix .= "<body onload=\'window.focus();\'>";
  22.     $sPrefix .= "<table>";
  23.     $sPrefix .= "<tr>";
  24.     $sPrefix .= "</tr>";
  25.     $sPrefix .= "<tr>";
  26.     $sPrefix .= "<td colspan=\'$intColSpan\'></td>";
  27.     $sPrefix .= "</tr>";
  28.     $sPrefix .= "</table>";
  29.  
  30.             $sReport = "<br>";
  31.            $sReport .= "enter what you want here";
  32.              $sReport .= "<br>";
  33.  
  34.              $sReport .= "</html>";
  35.  
  36.  
  37.             $sExcel = $sPrefix . $sReport;
  38.  
  39.     foreach(glob("*.xls") as $objExcelFile){
  40.         @ unlink("$objExcelFile");
  41.     }
  42.  
  43.     $sFileName = date("YmdHis") . ".xls";
  44.     $handle = fopen($sFileName, 'w');
  45.     fwrite($handle, $sExcel);
  46.     fclose($handle);
  47. $filepath=$sFileName;
  48.  
  49.  
  50.  
  51.  


You see its easy no need to use those exports....
Oct 30 '07 #5
Ok here's the code I'm using:


Expand|Select|Wrap|Line Numbers
  1.     $sPrefix = "<html xmlns:x=\'urn:schemas-microsoft-com:office:excel\'>";
  2.     $sPrefix .= "<head>";
  3.     $sPrefix .= "<!--[if gte mso 9]><xml>";
  4.     $sPrefix .= "<x:ExcelWorkbook>";
  5.     $sPrefix .= "<x:ExcelWorksheets>";
  6.     $sPrefix .= "<x:ExcelWorksheet>";
  7.     $sPrefix .= "<x:Name>Department name!!!!</x:Name>";
  8.     $sPrefix .= "<x:WorksheetOptions>";
  9.     $sPrefix .= "<x:Print>";
  10.     $sPrefix .= "<x:ValidPrinterInfo/>";
  11.     $sPrefix .= "</x:Print>";
  12.     $sPrefix .= "</x:WorksheetOptions>";
  13.     $sPrefix .= "</x:ExcelWorksheet>";
  14.     $sPrefix .= "</x:ExcelWorksheets>";
  15.  
  16.     $sPrefix .= "</x:ExcelWorkbook>";
  17.     $sPrefix .= "</xml>";
  18.     $sPrefix .= "<![endif]-->";
  19.     $sPrefix .= "<title>Tracking report</title>";
  20.     $sPrefix .= "</head>";
  21.     $sPrefix .= "<body onload=\'window.focus();\'>";
  22.     $sPrefix .= "<table>";
  23.     $sPrefix .= "<tr>";
  24.     $sPrefix .= "</tr>";
  25.     $sPrefix .= "<tr>";
  26.     $sPrefix .= "<td colspan=\'$intColSpan\'></td>";
  27.     $sPrefix .= "</tr>";
  28.     $sPrefix .= "</table>";
  29.  
  30.             $sReport = "<br>";
  31.            $sReport .= "enter what you want here";
  32.              $sReport .= "<br>";
  33.  
  34.              $sReport .= "</html>";
  35.  
  36.  
  37.             $sExcel = $sPrefix . $sReport;
  38.  
  39.     foreach(glob("*.xls") as $objExcelFile){
  40.         @ unlink("$objExcelFile");
  41.     }
  42.  
  43.     $sFileName = date("YmdHis") . ".xls";
  44.     $handle = fopen($sFileName, 'w');
  45.     fwrite($handle, $sExcel);
  46.     fclose($handle);
  47. $filepath=$sFileName;
  48.  
  49.  
  50.  
  51.  


You see its easy no need to use those exports....
thanks anyway.

but then my code can be used for exporting any of the tables in my database without having to generate a lot of files.
Oct 31 '07 #6
dafodil
392 256MB
The code customizes the table inside the excel in any layout you want, not generate a lot of files.

You can remove the detail part and just get sql query. There is also one advantage in the code I used. The code will search any .xls file inside a specific directory and reppace it with one .xls . You see, you can save space by doing that, assuming you are using the memory of your server to store files.
I wonder why you didn't post your code below. It seems that you copied the code I've given. lol. The hardest part is admitting it.

Just kidding...
Oct 31 '07 #7
The code customizes the table inside the excel in any layout you want, not generate a lot of files.

You can remove the detail part and just get sql query. There is also one advantage in the code I used. The code will search any .xls file inside a specific directory and reppace it with one .xls . You see, you can save space by doing that, assuming you are using the memory of your server to store files.
I wonder why you didn't post your code below. It seems that you copied the code I've given. lol. The hardest part is admitting it.

Just kidding...
For all I know, you copied that source code from another forum and took the ownership of it. It just so happened that you understood the code so it seemed like you made it.

Do not insult me. You have no right.

If that really is your code, explain how it can be customized so that it maybe used to export different tables from a database.
Oct 31 '07 #8
dafodil
392 256MB
Stop acting! Your act stinks! I already read all your posts and your doing the same thing when you want somebody to supply you the code. You tell them that they're insulting you.



Common scriptkiddy, is that how far you can go? Prove to me your worthy of being called a programmer.

I already gave you the code. Its already easy to use the sql codes.


I'm not biting into your tricks. I know that, I created that code. I'm just here to help not to create the whole logic of it. Post your code if you're not content with my code.
Oct 31 '07 #9
Hi Gee,

I was able to export data to an Excel file using the code below:
But, they all appear in a single cell with quotation marks (inverted commas) around each cell field.
Pls, help me as I need this code very urgently.
I want someone to tell me want I've not done and what I've done wrong.
PLEASE HELP ME.

-'Bunmi.


include('db.php');
$result = mysql_query('select * from table_name);
$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/vnd.ms-excel; name='excel'");

header("Content-type: application/octet-stream");

header("Content-Disposition: attachment; filename=Pictures.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n\n\t".$data;

//print "done";

?>
Apr 4 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
4
by: Jae | last post by:
I'm writing a web application that exports and imports excel files. The application gets a list of users and their info and displays it in a datagrid .The user then selects to save the file as a...
8
by: Taffman | last post by:
I've searched this goup for an answer to this, there are many discussions that come close but non that I can find that actually addresses this particular problem. I'm exporting queries to Excel....
1
by: mark.heyden | last post by:
Hi Frends, I am facing problems while trying to export data into pipe format. In fact I am fetching records from mysql database and then trying to create a text file ( using pipe as delimeter) ,...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
2
by: musa.biralo | last post by:
Hi, i want to read each row of specific column of musa.dbf using macro (VBA) in excel. my main gole is to copy the data of dbf file to excel file. I am new but want to do something..i tried and...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
2
by: homerfavenir | last post by:
i have to export data from mysql to excel, but i dont know the script. please could anyone help me how to do this. thanks
2
by: Nosferatum | last post by:
Hi, is it possible to export mysql data to an excel spreadsheet? By PHP and with a structured, nice layout?
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.