Example: Viewing Database Information Over the Web
Before we can view database information we need to create an msql table and add some data to it. (Later we will add data to our tables using html forms and cgi scripts)
msql webdev
create table <table_name>
(first_name char(15) not null,
last_name char(15) not null,
age int) \g
You should replace <table_name> with whatever name you want your table to have. The char(15) which follows first_name and last_name tells msql that you will be storing strings up to 15 characters long in these two columns. The int which follows age tells msql that you will be storing integer values in this column. See the msql quick reference page for all the available data types msql uses. The not null which follows first_name and last_name tells Msql that every entry in our table must have data in these two columns. The \g tells msql that you want to execute the command.
Your table has been created. It is now time to add data to that table.
insert into <table_name>
values ('John', 'Smith', 42) \g
and
insert into <table_name>
(first_name, last_name)
values ('Bill', 'Johnson') \g
Notice the second insert statement does not contain an entry for the age column. This is okay because when we created the table we did not put not null after age. Make a few more entries with whatever names and ages you want.
select * from <table_name> \g
or
select first_name from <table_name>
where age = 42 \g
Try several different select statements to see the different ways you can access your data. See the quick reference page for a complete display of select statement options. Try using order by to cause your data to be displayed in a specific order. Later all these msql statements will be imbedded in Perl scripts.
There are other Msql statements which I haven't mentioned in this example. Feel free to try out the other commands as well. You will find these commands on the Msql quick reference page or on the Msql home page.
\q
mkdir <directory_name>
Replace <directory_name> with whatever you want to call your directory. This is where you will put your Perl scripts. Currently, only the webdev home directory is recognized by the server as an valid place from which to run cgi-scripts. Once you have created a directory for yourself send me mail and I will activate your directory as well. Until your dirctory has been activated you can run your scripts from the webdev home directory.
This system is fairly limited in its available editors, so I recommend that you write your Perl scripts on another machine, using your favorite editor, and then transfer your files to this directory. If you dont know how to do this, send me mail and I will tell you.
#!/usr/local/bin/perl
use Msql;
my $dbh = Msql->connect;
$dbh->selectdb('webdev');
my $sth = $dbh->query(qq[select * from whitney]);
print("Content-type: text/html\n\n");
print "
<html>
<body>
<table border=\"1\" width=\"727\">\n";
my %hash;
while (%hash = $sth->fetchhash)
{
print "
<tr>
<td align=\"center\">$hash{first_name}</td>
<td align=\"center\">$hash{last_name}</td>
</tr>\n";
}
print "
</table>
</body>
</html>\n";
http://www-td.fnal.gov/webdev/<directory_name>/<script_name>
If you are running your script from the webdev home directory you will have no <directory_name>. If you are running perl code from the directory you created for yourself, replace <directory_name> with the name of your directory. In either case replace <script_name> with the name of your script.