[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]
It is the last two steps which make CGI scripts so useful. The program can perform what ever operations it needs to and it can then generate a .html page based on the results of these operations. When the CGI script is used with a database such as MySQL, many things are possible. Generally, the page returned to the user's browser contains the results of the database search. Or, if the user had provided information through a form in the web page, the database records were updated.
- User clicks on a link in a web page (e.g. http://www.cgitest.com/cgi-bin/test.cgi).
- The web server runs the program test.cgi.
- The test.cgi program does what it is programmed to do.
- The test.cgi program also builds a .html file in memory and sends it back to the user's browser.
All queries follow the same basic formula. Simply replace the SELECT statement with the INSERT, UPDATE, DELETE, etc. statement you wish to use. Note that these other queries do not return records. So, the fetchrow() and assignment which follows should be deleted for then.# Use the DBI module use DBI qw(:sql_types); # Declare local variables my ($databaseName, $databaseUser, $databasePw, $dbh); my ($stmt, sth, @newRow); my ($telephone); # Set the parameter values for the connection $databaseName = "DBI:mysql:yourWebSite_co_nz"; $databaseUser = "yourLoginId"; $databasePw = "yourLoginPassword"; # Connect to the database # Note this connection can be used to # execute more than one statement # on any number of tables in the database $dbh = DBI->connect($databaseName, $databaseUser, $databasePw) || die "Connect failed: $DBI::errstr\n"; # Create the statement. $stmt = "SELECT Name FROM Phonebook WHERE (Telephone LIKE '518%')"; # Prepare and execute the SQL query $sth = $$dbh->prepare($$stmt) || die "prepare: $$stmt: $DBI::errstr"; $sth->execute || die "execute: $$stmt: $DBI::errstr"; # Get the first record # If more than one record will be returned put # the fetchrow in a while loop @record = $sth->fetchrow() # Get the value of the first field returned. $telephone = $record[0]; # Clean up the record set and the database connection $sth->finish(); $dbh->disconnect();