| |||||||||||
| |||||||||||
DBI used in a CGI script as a GUI to a table
Using SQL Databases from Perl example from a Well House Consultants training course
More on Using SQL Databases from Perl [link] Source code: orange.pl Module: P308
#!/usr/bin/env perl
# A sample program that looks up information in a MySQL # database and presents it through a web page. When # first called up (or run without input) it lists out # all possible destinations. When run with a completed # form, reports full details on all trains that match the # given destination. # Uses Perl's DBI and DBD-MySQL modules from thge CPAN # Requires access to MySQL. Sample SQL commands to set up # appropriate table populate it at base of this example print "content-type: text/html\n\n"; print "<center><h1>Mighty Fine Database Stuff</h1></center>"; print <<"HTMLHEAD"; <form>Enter Destination Wanted <input name=destin></form><br> HTMLHEAD use DBI; # Technical note - need to URLdecode $dl if this script is # used in anything except a benign environment ($dl) = $ENV{QUERY_STRING} =~ /.*=(.*)/; $dbtype = "DBI:mysql"; $host = "192.168.200.66"; $user = "trainee"; $password = "abc123"; $dbase = "graham"; $dbh = DBI->connect("${dbtype}:${dbase}:${host}",$user,$password); # If the user has entered a destination, report on that place if ($dl) { $qhandle = $dbh -> prepare("select tid, destination, length, time ". "from train where destination like '%$dl%' order by tid desc"); $qhandle -> execute(); $qhandle -> bind_columns(undef, \$tid, \$destiny, \$coaches, \$timedat); print "<table border=0 cellpadding=4>"; while (@gotten = $qhandle->fetchrow()) { print "<tr>"; for $val (@gotten) { print "<td>$val</td>"; } print "</tr>"; } print "</table>"; } else { # If the user has NOT entered a destination, list all possibles $qhandle = $dbh -> prepare("select distinct destination from train"); $qhandle -> execute(); while (@gotten = $qhandle->fetchrow()) { print "You could choose $gotten[0]<br>"; } } $dbh -> disconnect(); $yr = 1900 + (localtime())[5]; print <<"ENDING"; <hr> Copyright $yr<br> Support from .... ENDING __END__ Sample data for the above program - taken by mysqldump -- -- Table structure for table `train` -- CREATE TABLE train ( time text, length int(11) default NULL, destination text, tid int(11) NOT NULL auto_increment, PRIMARY KEY (tid) ) TYPE=MyISAM; -- -- Dumping data for table `train` -- INSERT INTO train VALUES ('07:05',1,'Salisbury',1); INSERT INTO train VALUES ('08:18',2,'Swindon',2); INSERT INTO train VALUES ('09:05',2,'Southampton',3); INSERT INTO train VALUES ('05:45',1,'Swindon',4); INSERT INTO train VALUES ('10:35',12,'Plymouth',10); INSERT INTO train VALUES ('13:49',2,'Swindon',6); INSERT INTO train VALUES ('14:20',2,'Salisbury',7); INSERT INTO train VALUES ('17:07',1,'Swindon',8); INSERT INTO train VALUES ('18:18',1,'Salisbury',9); INSERT INTO train VALUES ('13:00',6,'Windsor',11); INSERT INTO train VALUES ('17:46',1,'Swindon',12); INSERT INTO train VALUES ('17:46',1,'Swindon',13); INSERT INTO train VALUES ('19:30',1,'Westbury',14); INSERT INTO train VALUES ('19:30',3,'Westbury',15); INSERT INTO train VALUES ('18:15',4,'Slough',16); INSERT INTO train VALUES ('18:17',1,'Audley End for Saffron Walden',17); INSERT INTO train VALUES ('18:20',1,'Besses O\"the Barn',18); INSERT INTO train VALUES ('18:20',1,'Tam O\'Shanter Road, Edinburg',19); Learn about this subject
This module and example are covered on the following public courses:
* MySQL * Perl for Larger Projects Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Perl are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Using SQL Databases from Perl" training module. You'll find a description of the topic and some
other closely related examples on the "Using SQL Databases from Perl" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |