473,400 Members | 2,163 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,400 software developers and data experts.

MySQL database to excel spreadsheet report

Hi again my great online assistants, just would like to say that i'm testing the script under unix , and after researches on the internet, i have realised that to resolve 500 Internal Server Error, we should put the script in cgi-bin directory, use of ASCII transfer mode, chmod 755 scriptname.cgi, and then login to unix and type ./scriptname.cgi.

I have connected to the database using mysql -u***** -p***** bugs
and i could have access to the database from mysql
I have done all of those but the internet page is always displaying 500 Internal Server Error.The server encountered an internal error or misconfiguration and was unable to complete your request.

and in unix i had the following:
Unrecognized character \x93 at ./perlcode1.cgi line 11.

But i think that the connection syntax is correct
what to do please?

Best Regards.

The code is the following:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -W
  2. use DBI;
  3. use DBD::mysql;
  4. use strict;
  5. use File::Copy;
  6. use File::Path;
  7. use File::Basename;
  8. use Spreadsheet::WriteExcel::Big;
  9. use Data::Dumper;
  10.  
  11. my $worksheet = ['Report.xls'];
  12. my $dbh = DBI->connect( "DBI:mysql:dbname=bugs:localhost", "carole", "carole", { RaiseError => 1, PrintError => 0, AutoCommit => 0 } )
  13.     or die("Error:$DBI::errstr");
  14. my $excel_file = "C:/Perl/Report.xls";
  15. $query = qq{SELECT 
  16. b.bug_id, b.bug_file_loc, b.bug_severity, b.bug_status,
  17. b.creation_ts, b.delta_ts, b.short_desc, b.op_sys, 
  18. b.priority, b.rep_plarform, b.version, b.component_id,
  19. b.resolution, b.target_milestone, b.status_whiteboard, b.votes, 
  20. b.keywords, b.last_diffed, b.everconfirmed, b.reporter_accessible,
  21. b.cclist_accessible, b.estimated_time, b.remaining_time, b.deadline,
  22. b.alias, pf.login_name, pd.name 
  23. FROM bugs b, profiles pf, products pd
  24. WHERE bugs.assigned_to = profiles.user_id
  25. AND bugs.product_id = products.id
  26. ORDER BY bug_id};
  27. my $sth = $dbh->prepare($query);
  28. $sth->execute() or die $dbh->errstr;
  29. while ( my $ref = $sth->fetchrow_hashref() ) {
  30.     print $ref ->{
  31.         'bug_id', 'assigned_to', 'bug_file_loc', 'bug_severity',
  32.         'bug_status', 'creation_ts', 'delta_ts', 'short_desc',
  33.         'op_sys', 'priority', 'product_id', 'rep_plarform',
  34.         'version', 'component_id', 'resolution', 'target_milestone',
  35.         'status_whiteboard', 'votes', 'keywords', 'last_diffed',
  36.         'everconfirmed', 'reporter_accessible', 'cclist_accessible', 'estimated_time',
  37.         'remaining_time', 'deadline', 'alias', 'login_name',
  38.         'name'
  39.       },
  40.       "\n";
  41. }
  42. # -e checks to see if the file exists
  43.  
  44. if (-e $excel_file) {
  45.     #open Excel file
  46.     my $Book = $Excel->Workbooks->Open($excel_file);
  47.     $exists = "true";
  48.  
  49. } else {
  50.     $Book   = $Excel->Workbooks->Open($excel_file);
  51.     $exists = "true";
  52.  
  53. } else {
  54.     $Book   = $Excel->Workbooks->Add();
  55.     $exists = "false";
  56. }
  57.  
  58. if ( $exists eq "true" ) {
  59.     $previousSheet             = $Book->ActiveSheet;
  60.     $currentSheet              = $Book->Worksheets->Add();
  61.     $Book->ActiveSheet->{Name} = "Report";
  62.  
  63. } else {
  64.     $previousSheet = 0;
  65. }
  66.  
  67. #create the spreadsheet
  68. $excel->output();
  69.  
  70. #sort data of worksheet – ASC or DESC
  71. $excel->sort_data( "Report.xls", 0, "ASC" );
  72.  
  73. #add headers to 'Report'
  74. $excel->set_headers( 'Report.xls', [qw/Bugs report/] );
  75.  
  76. #print sheet-names
  77. print join( ", ", $excel->sheets() ), "\n";
  78.  
  79. #get the result as a string
  80. my $spreadsheet = $excel->output_as_string();
  81.  
  82. #print result into a file and handle error
  83. $excel->output_to_file("Report.xls") or die $excel->errstr();
  84. if ( $exists eq "true" ) {
  85.     $Book->Save();
  86.  
  87. } else {
  88.     $Book->SaveAs("$excel_file") or die $!;
  89. }
  90. $sth->finish();
  91. $dbh->disconnect();
  92.  
Aug 22 '07 #1
5 5067
miller
1,089 Expert 1GB
Hello Sport Girl,

Your code, as you originally posted it, contained a lot of unrecognizable characters. I'm guessing that they may be unicode or some other special character set, and that is leading to problems with running your script on the server. I suspect that the problem is either in the editor that you're using or the method in which you're transferring the file to your web server.

I've translated all the special character to their likely intended character in your above post. I've also run your code through Perl Tidy in order to clean up your formatting. You may now notice an error in your code where you have two "else" statements.

Anyway, I suggust that you figure out what is causing your character set issue before trying to do anything else. That is likely leading to problems throughout your code base, not just with this one script.

- Miller
Aug 22 '07 #2
Hi Miller, thank u very much 4 trying to helping me. I appreciate it very much.

In fact i have done what you have told me and changed the character set to ASCII in my local machine where i am supposed to do the testing but the error 500 Internal Server Error is still persisting.
I don't know if the fact is that i am not given permissions or priveleges to change in the configuration as a trainee, so they do not take effects or it's just i can't test my new code for security reasons. I have talked to my consultant about it but....
Anyway i'll post the new code , the error on the internet page is: Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

and on unix when trying to do ./scriptname.cgi is:
Unrecognized character \x93 at ./scriptname.cgi line 11.

I have done under unix "Setup- file transfer" and the following message appears:
Unable to transfer file: Host does not respond- Could not connect.

But I am logged in in unix with a username and password(i am using Reflection - Host-Unix and Digital) and i am connected in MySQL to the database( i am using Reflection - Reflection X) and i am connected to the FTP in a email address( i am using Reflection - FTP Client). I checked the ASCII character set in the Unix and FTP windows.

So could it be security or configuration reasons or what else should i do?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl -W
  3. use DBI;
  4. use DBD::mysql;
  5. use strict;
  6. use File::Copy;
  7. use File::Path;
  8. use File::Basename;
  9. use Spreadsheet::WriteExcel::Big;
  10. use Data::Dumper;
  11. my $worksheet = ['Report.xls'];
  12. my $dbh = DBI->connect(DBI:mysql:dbname=bugs:localhost, carole, carole, { RaiseError=>1, PrintError=>0, AutoCommit=>0}) or die(Error:DBI::errstr);
  13. my $excel_file = ‘C:/Perl/Report.xls’;
  14. $query =qq{SELECT
  15. b.bug_id, b.bug_file_loc, b.bug_severity, b.bug_status, b.creation_ts, b.delta_ts, b.short_desc, b.op_sys, b.priority, b.rep_plarform, b.version, b.component_id, b.resolution, b.target_milestone, b.status_whiteboard, b.votes, b.keywords, b.last_diffed,
  16. b.everconfirmed, b.reporter_accessible, b.cclist_accessible, b.estimated_time, b.remaining_time, b.deadline,  b.alias, pf.login_name, pd.name
  17. FROM bugs b, profiles pf, products pd
  18. WHERE bugs.assigned_to = profiles.user_id
  19. AND bugs.product_id = products.id
  20. ORDER BY bug_id;};
  21. my $sth = $dbh -> prepare($query);
  22. $sth ->execute() or die $dbh ->errstr;
  23. while ( my $ref = $sth->fetchrow_hashref()) {
  24.     print $ref ->{
  25. 'bug_id', 'assigned_to', 'bug_file_loc', 'bug_severity', 'bug_status', 'creation_ts', 'delta_ts', 'short_desc', 'op_sys', 'priority', 'product_id', 'rep_plarform', 'version', 'component_id', 'resolution', 'target_milestone', 'status_whiteboard', 'votes',
  26.  'keywords', 'last_diffed', 'everconfirmed', 'reporter_accessible', 'cclist_accessible', 'estimated_time', 'remaining_time', 'deadline',  'alias', 'login_name', 'name'}, "\n";
  27. }
  28. //-e checks to see if the file exists
  29. if (-e $excel_file)
  30. {
  31. #open Excel file
  32. my $Book = $Excel->Workbooks->Open($excel_file);
  33. $exists="true";
  34. }else{
  35. $Book = $Excel->Workbooks->Add();
  36. $exists = "false";
  37. }
  38. if($exists eq "true"){
  39.   $previousSheet = $Book->ActiveSheet;
  40.   $currentSheet = $Book->Worksheets->Add();
  41.   $Book->ActiveSheet->{Name}="Report";
  42. }else{
  43.   $previousSheet = 0;
  44. }
  45. #create the spreadsheet
  46. $excel -> output();
  47. #sort data of worksheet – ASC or DESC
  48. $excel -> sort_data(‘Report.xls’,0, ‘ASC’);
  49. #add headers to 'Report'
  50. $excel -> set_headers('Report.xls', [qw/Bugs report/]);
  51. #print sheet-names
  52. print join( , ,$excel -> sheets()), \n;
  53. #get the result as a string
  54. my $spreadsheet = $excel -> output_as_string();
  55. #print result into a file and handle error
  56. $excel->output_to_file(Report.xls) or die $excel -> errstr();
  57. if($exists eq "true"){
  58.   $Book->Save();
  59. }else{
  60.   $Book->SaveAs("$excel_file") or die $!;
  61. }
  62. $sth -> finish();
  63. $dbh -> disconnect();
  64.  
  65.  
Aug 27 '07 #3
Hi everybody, just want to say that i have resolved the problem of the character set but i have syntax errors:

can somebody help me how can i rerieve data from multilple database tables cause my query syntax is wrong: i need to retrieve data from the tables bugs, profiles and products from the database bugs.

Please can somebody help, i really need it?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl -W
  3. use DBI;
  4. use DBD::mysql;
  5. use strict;
  6. use File::Copy;
  7. use File::Path;
  8. use File::Basename;
  9. use Spreadsheet::WriteExcel::Big;
  10. use Data::Dumper;
  11. my $worksheet = ['Report.xls'];
  12. my $dbh = DBI->connect("dbi:mysql:database=bugs;host=localhost") or die "Connection Error";
  13. my $excel_file = 'C:/Perl/Report.xls';
  14. $query =qq{SELECT
  15. b.bug_id, b.bug_file_loc, b.bug_severity, b.bug_status, b.creation_ts, b.delta_ts, b.short_desc, b.op_sys, b.priority, b.rep_plarform, b.version, b.component_id, b.resolution, b.target_milestone, b.status_whiteboard, b.votes, b.keywords, b.last_diffed,
  16. b.everconfirmed, b.reporter_accessible, b.cclist_accessible, b.estimated_time, b.remaining_time, b.deadline,  b.alias, pf.login_name, pd.name
  17. FROM bugs b, profiles pf, products pd
  18. WHERE bugs.assigned_to = profiles.user_id
  19. AND bugs.product_id = products.id
  20. ORDER BY bug_id;};
  21. my $sth = $dbh -> prepare($query);
  22. $sth ->execute() or die $dbh ->errstr;
  23. while ( my $ref = $sth->fetchrow_hashref()) {
  24.     print $ref ->{
  25. 'bug_id', 'assigned_to', 'bug_file_loc', 'bug_severity', 'bug_status', 'creation_ts', 'delta_ts', 'short_desc', 'op_sys', 'priority', 'product_id', 'rep_plarform', 'version', 'component_id', 'resolution', 'target_milestone', 'status_whiteboard', 'votes',
  26.  'keywords', 'last_diffed', 'everconfirmed', 'reporter_accessible', 'cclist_accessible', 'estimated_time', 'remaining_time', 'deadline',  'alias', 'login_name', 'name'}, "\n";
  27. }
  28. if (-e $excel_file)
  29. {
  30. #open Excel file
  31. my $Book = Workbooks->Open($excel_file);
  32. $exists="true";
  33. }else{
  34. $Book =Workbooks->Add();
  35. $exists = "false";
  36. }
  37. if($exists eq "true"){
  38.   $previousSheet = $Book->ActiveSheet;
  39.   $currentSheet = $Book->Worksheets->Add();
  40.   $Book->ActiveSheet->{Name}="Report";
  41. }else{
  42.   $previousSheet = 0;
  43. }
  44. #create the spreadsheet
  45. $excel_file -> output();
  46. #sort data of worksheet  ASC or DESC
  47. $excel -> sort_data('Report.xls',0,'ASC');
  48. #add headers to 'Report'
  49. $excel -> set_headers('Report.xls', [qw/Bugs report/]);
  50. #get the result as a string
  51. my $spreadsheet = $excel_file -> output_as_string();
  52. #print result into a file and handle error
  53. $excel->output_to_file('Report.xls') or die $excel -> errstr();
  54. if($exists eq "true"){
  55.   $Book->Save();
  56. }else{
  57.   $Book->SaveAs("$excel_file") or die $!;
  58. }
  59. $sth -> finish();
  60. $dbh -> disconnect();
  61.  
  62.  
Aug 30 '07 #4
numberwhun
3,509 Expert Mod 2GB
Unfortunately, I don't have tons of experience with DBI or SQL so I would have to look into your question. But, I did want to add this, instead of using the "-W" or "-w" switch to the shebang line, you should really use the "use warnings;" pragma instead. Just standard best practice is all.

Regards,

Jeff
Aug 30 '07 #5
Hi Jeff,
thank u for ur cooperation .I appreciate it very much.
Actually i have made a step in syntax errors but still there is something to do:

in fact i can't get the data retrieved from the bugs, products and profiles tables written in the excel sheet file.
How can i get the data retreived written to the excel sheet unless from the table bugs?

I'll give details:

the database name is bugs

the fields in the table bugs are:

bug_id
bug_file_loc
$bug_severity
bug_status
creation_ts
delta_ts
short_desc
op_sys
priority
rep_platform
version
component_id
resolution
target_milestone
status_whiteboard
votes
keywords
lastdiffed
everconfirmed
reporter_accessible
cclist_accessible
estimated_time
remaining_time
deadline
my $alias

The field in the profiles table to be retrieved: login_name
The field in the products table to be retreived is : name

Can u give me a hand with this one?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl 
  3. use strict;
  4. use warnings;
  5. use DBI;
  6. use DBD::mysql;
  7. use lib qw(.);
  8. use Bugzilla;
  9. use Bugzilla::Bug;
  10. use Bugzilla::Util;
  11. use Bugzilla::Constants;
  12. use Time::Local;
  13. use Spreadsheet::WriteExcel;
  14.  
  15.  
  16. my $dbh = DBI->connect('DBI:mysql:database=bugs;host=localhost', 'jinny', 'jinny') or die "Connection Error";
  17.  
  18. my $Excelfile = "/tmp/Report.xls";
  19.  
  20. #create a new instance
  21. my $excel = Spreadsheet::WriteExcel->new("$Excelfile");
  22. my $worksheet = $excel->addworksheet("Bugs Report");
  23. my $now = localtime time;
  24. $worksheet->write(0, 0, "Report generated on :$now" );
  25.  
  26. my $stmt = "SELECT b.bug_id, b.bug_file_loc, b.bug_severity, b.bug_status, b.creation_ts, b.delta_ts, b.short_desc, b.op_sys, b.priority, b.rep_platform, b.version, b.component_id, b.resolution, b.target_milestone, b.status_whiteboard, b.votes, b.keywords, b.lastdiffed,
  27. b.everconfirmed, b.reporter_accessible, b.cclist_accessible, b.estimated_time, b.remaining_time, b.deadline,  b.alias, pf.login_name, pd.name
  28. FROM bugs b, profiles pf, products pd
  29. ORDER BY bug_id";
  30.  
  31.  
  32. my $sth = $dbh->prepare($stmt);
  33. my @data;
  34.  
  35. $sth->execute() or die $dbh->errstr;
  36.  
  37. while ( @data = $sth->fetchrow_array()){
  38.     my $bug_id = $data[1];
  39.     my $bug_file_loc = $data[2];
  40.     my $bug_severity = $data[3];
  41.     my $bug_status = $data[4];
  42.     my $creation_ts = $data[5];
  43.     my $delta_ts = $data[6];
  44.     my $short_desc = $data[7];
  45.     my $op_sys = $data[8];
  46.     my $priority = $data[9];
  47.     my $rep_platform = $data[10];
  48.     my $version = $data[11];
  49.     my $component_id = $data[12];
  50.     my $resolution = $data[13];
  51.     my $target_milestone = $data[14];
  52.     my $status_whiteboard = $data[15];
  53.     my $votes = $data[16];
  54.     my $keywords = $data[17];
  55.     my $lastdiffed = $data[18];
  56.     my $everconfirmed = $data[19];
  57.     my $reporter_accessible = $data[20];
  58.     my $cclist_accessible = $data[21];
  59.     my $estimated_time = $data[22];
  60.     my $remaining_time= $data[23];
  61.     my $deadline = $data[24];
  62.     my $alias= $data[25];
  63.     my $login_name = $data[26];
  64.     my $name = $data[27];
  65.     my $row = 0;
  66.     my $col = 0;
  67.     foreach my $stmt (@data) {
  68.         $worksheet->write($row++, @data);
  69.         last;
  70.     }
  71. }
  72.  
  73. $sth->finish();
  74. $dbh->disconnect();
  75.  
  76.  
Aug 30 '07 #6

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

Similar topics

8
by: Johnny | last post by:
I was looking for information on how to import an excel file or other text file into a MySql database. I have done this with both access and SQL Server and am looking for a way to do this in...
3
by: bawar | last post by:
Hey, I can say I belong to the newbies in this subject. I own my own website with mySQL-databases, but now I have to show one table of those mySQL-databases in an excel worksheet. It is the...
4
by: ctkevin | last post by:
I am an experienced Access user but recently moved an Access database to the web for my employer. We use a host that does not allow the use of data access pages. Is there anyway to pre-build a...
2
by: Anne Sachleben via AccessMonster.com | last post by:
I am using the TransferSpreadsheet function to export a query result to a specific worksheet in an Excel file titled "report". I want the result to be exported to the worksheet titled "facts". ...
6
by: DeniseY | last post by:
I have an Access report that is created on the fly by the user selecting the fields to be included. The Access report comes out fine, but I want it to automatically output to an Excel spreadsheet....
2
by: nofear | last post by:
I used to export my reports as snapshot but now I have to export them to Excel When I export my report to a Excel Spreadsheet the report header and footer are not included Only the data gets...
5
by: Sport Girl | last post by:
Hi everybody. I am new in PERL language and working in it since little hours, actually i am still a trainee and i need help please. In fact, i need a script in PERL that enables me to retrieve...
1
by: Sport Girl | last post by:
Hi everybody , i have the task of developing in Perl a script that retrieves data from 3 tables ( bugs, profiles, products) from a MySQL database called bugs and display them in an excel sheet...
1
by: Master Ken | last post by:
Hi All, I'm very new to C# and ASP and I am asking for some help as I don't really know where to start on this project. Some background info first I run a report each week which outputs data...
2
by: Alan M Dunsmuir | last post by:
A client has sent me a (Windows) Excel Spreadsheet (.xls) file containing a block of data he wants included as a table in a MySQL database in a (Linux-based) PHP/MySQL Web application I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.