Database Reference
In-Depth Information
This time we send the prepare function with the query to the database handler, and store
the results in a set handler variable. If there are any errors in the query, this function will
terminate before the query is actually executed
Finally, we can execute the query by sending the execute function to the set handler:
$sth->execute() || quit();
Processing the Query Results
After successful execution of the query, the set handler variable will contain a set of records
that we can process. As with PHP there are many ways in which you can do this with Perl.
We will use a function similar to our PHP example, which returns the current row of the
record set as an indexed array. This is called fetchrow_array() and is sent to the set handler
as follows:
myarray = $sth->fetchrow_array()
When we have a row inside the array, we use an index number to retrieve the value of
that index. It is useful for us to store this value in a more meaningful variable that we can
use while printing, so we can use the following to do this:
my $pagetitle = $myarray[0];
The two commands above on their own will only get the information from one row, so to
use this on a multiple-row query we need to wrap it up in a control structure as follows:
while (@myarray = $sth->fetchrow_array()) {
my $pagetitle = $myarray[0];
my $ipnumber = $myarray[1];
my $datecreated = $myarray[2];
print “$datecreated $pagetitle $ipnumber<BR>\n”;
}
This will loop through each row of the record set, retrieving, processing and printing the
row each time. It will stop once the last row has been processed.
All that remains is to finish off the HTML file as follows:
print”<HR>Ok”;
print”</BODY></HTML>\n”;
I have included the OK line to indicate that the script has got to the end without error. If
an error occurs anywhere on the script, you can often get just a blank document or a half-
completed webpage returned. If you are writing a Perl script incrementally and testing each
part, it is often useful to put an indicator like this on the last line so that you can see whether
your last piece of code has executed successfully or not.
Search WWH ::




Custom Search