Important alert: (current site time 7/15/2013 8:54:50 PM EDT)
 

VB icon

Generate HTML from MySQL

Email
Submitted on: 6/1/2001 1:53:18 AM
By: RyMon  
Level: Intermediate
User Rating: By 3 Users
Compatibility: 5.0 (all versions), 4.0 (all versions), 3.0 (all versions)
Views: 17665
(About the author)
 
     This will grab an entire table from a MySQL database and generate an html table from it
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Generate HTML from MySQL
= Description:This will grab an entire table from a MySQL database and generate an html table from it
= By: RyMon
=
= Inputs:Arguments: database name, table name, html file to generate (will default to STDOUT), username, and password
=
= Returns:An HTML file, with one big table
=
= Assumes:Assumes you have the DBI::MySQL stuff installed and running at your site.
This will grap ALL the columns from a table, but it would be relatively easy to make it selective
=
= Side Effects:N/A
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=197&lngWId=6=for details.=**************************************

#!perl
use DBI;
#Get arguments from command line
($db,$table,$out_file,$user,$pass)=@ARGV;
#Check for invalid info
unless($db && $table) {
	print <<EOUsage;
Usage: $0 <database> <table> <out file> <user> <pass>
\tUnless you specify out file, STDOUT will be used.
\t(Sending an out file of '-' will generate the same result)
\tUser and Pass are optional.
EOUsage
	exit();
}
#No out file if '-'
undef $out_file if($out_file eq '-');
#Connect to the DBI using our db, user, and pass
my $dbr=DBI->connect("dbi:mysql:$db",$user,$pass,{
		PrintError=>0
	}) or die "Can't connect to database; $DBI::errstr\n";
#Get all the rows from our table
my $sth=$dbr->prepare("SELECT * FROM $table")
	or die "Can't prepare SQL statement: $DBI::errstr\n";
#Execute our command to get the rows
$sth->execute or die "Can't execute: $DBI::errstr\n";
#Open out_file
if($out_file) {
	#Don't open file until now to avoid overwritting
	#	in case of errors
	open OUT,">$out_file" or die "Cannot open $out_file for writting: $!\n";
}
#Header html data
$head=<<EOHead;
<html>
<head>
<title>Database Summary</title>
</head>
<body>
<h1>Data in $db:$table</h1>
<table border=1>
EOHead
out($head);
#Print a table row for each database row
my @row;
while(@row=$sth->fetchrow_array()) {
	out("<tr><td>" . join("</td><td>",@row) . "</td></tr>\n");
}
#HTML tail info
$tail=<<EOTail;
</table>
</body>
</html>
EOTail
#Close out_file
if($out_file) {
	close OUT;
}
#Disconnect from the database
$dbr->disconnect;
#Use this sub to make it easier when using file || STDOU
sub out {
	my $data=shift;
	if($out_file) {
		print OUT $data;
	} else {
		print $data;
	}
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/24/2008 1:22:49 PMArmando

I copy paste the program but it is giving me an error message like this
Can't find string terminator "EOHead" anywhere before EOF at returnatable.pl line 33.

can you shed some light on these error?


(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.