📄 00000002.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: yyh (阿欢&正在努力上进中......), 信区: Linux <BR>标 题: MySQL & mSQL Chapter 10 Perl (3) <BR>发信站: BBS 水木清华站 (Sat Sep 18 01:44:22 1999) <BR> <BR>MysqlPerl <BR>Monty Widenius, the author of MySQL, also wrote the Perl interface to MySQL, Mysql.pm. This was based on the mSQL module, Msql.pm. Thus, the interfaces of the two modules are almost identical. In fact, we recently converted an entire site from mSQL to <BR>MySQL by running "perl -e 's/^Msql/Mysql/" *.cgi" in every directory containing a CGI. This covers 95% of the work involved. Of course, this does not give you any of the advantages of MySQL, but it is a quick and easy way to start down the road to <BR>MySQL. Mysql.pm is maintained as part of msql-mysql-modules by Jochen Wiedmann. <BR> <BR>TIP: One of the largest differences between MySQL and mSQL is the way they handle sequences. In mSQL, a sequence is defined on a table with a command like CREATE SEQUENCE on tablename. The sequence value is then read as if it were a normal table value <BR>with the command SELECT _seq from tablename. MySQL adds the flag AUTO_INCREMENT to the primary key. Whenever a null value is inserted into this field, it is automatically incremented. Both MySQL and mSQL allow only one sequence per table. For a full <BR>discussion on sequences in MySQL and mSQL, see Chapter 6, SQL According to MySQL and mSQL. <BR> <BR>As an example of some of the features of Mysql.pm, let's go back to the tests example. Now that we have subject.cgi taken care of, the next step is the table of student information. The structure of the students table is as follows: <BR> <BR>CREATE TABLE student ( <BR> id INT NOT NULL auto_increment, <BR> first VARCHAR(50), <BR> middle VARCHAR(50), <BR> last VARCHAR(50), <BR> ext VARCHAR(50), <BR> subjects VARCHAR(100), <BR> age INT, <BR> sex INT, <BR> address BLOB, <BR> city VARCHAR(50), <BR> state VARCHAR(5), <BR> zip VARCHAR(10), <BR> phone VARCHAR(10), <BR> PRIMARY KEY (id) <BR>) <BR>All of the information used by the subject.cgi program is in this table, as well as other information pertaining to the student. The program that handles this table, student.cgi must perform all of the functions that subject.cgi did for the subject <BR>table. <BR> <BR>TIP: It is not possible to access a mSQL database with the Mysql.pm module, or MySQL with Msql.pm. The student.cgi program expects to find a MySQL version of the subjects table. Likewise, the subject.cgi program expects an mSQL version of the students <BR>table. <BR> <BR>To illustrate the operation of Mysql.pm, we will examine in detail the portion of student.cgi that enables a user to change the information about a student. Just like the "add" action in the Msql.pm example was broken up into four separate functions, <BR>the "change" action here is separated into three individual functions. <BR> <BR>The first function, change, prints out a form that allows the user to search for a student to change, as shown in the following: <BR> <BR>sub change { <BR> print header, start_html('title'=>'Student Change Search', <BR> 'BGCOLOR'=>'white'); <BR> &print_form('search2','Search for a Student to Change',1); <BR> print <<END_OF_HTML; <BR><p> <BR><INPUT TYPE=HIDDEN NAME="subaction" VALUE="change2"> <BR><INPUT TYPE=SUBMIT VALUE=" Search for Students "> <BR> <INPUT TYPE=SUBMIT NAME="all" VALUE=" View all Students "> <BR> <INPUT TYPE=RESET> <BR></form></body></html> <BR>END_OF_HTML <BR>} <BR>The form used for searching for a student to "change" is so similar to the form used to searching for a student to "view" and the one to "add" a student that a single function, print_form, is used for all three purposes, as shown in the following: <BR> <BR>sub print_form { <BR> my ($action,$message,$any) = @_; <BR> <BR> print <<END_OF_HTML; <BR><FORM METHOD=post ACTION="students.cgi"> <BR><INPUT TYPE=HIDDEN NAME="action" VALUE="$action"> <BR><H1>$message</h1> <BR>END_OF_HTML <BR> if ($any) { <BR> print <<END_OF_HTML; <BR><p>Search for <SELECT NAME="bool"> <BR><OPTION VALUE="or">any <BR><OPTION VALUE="and">all <BR></select> of your choices. <BR>END_OF_HTML <BR> } <BR> print <<END_OF_HTML; <BR><p> <BR>First: <INPUT NAME="first" SIZE=20> <BR> Middle: <INPUT NAME="middle" SIZE=10> <BR> Last: <INPUT NAME="last" SIZE=20> <BR> Jr./III/etc.: <INPUT NAME="ext" SIZE=5> <BR><br> <BR>Address: <INPUT NAME="address" SIZE=40><br> <BR>City: <INPUT NAME="city" SIZE=20> <BR> State: <INPUT NAME="state" SIZE=5> <BR> ZIP: <INPUT NAME="zip" SIZE=10><br> <BR>Phone: <INPUT NAME="phone" SIZE=15><br> <BR>Age: <INPUT NAME="age" SIZE=5> Sex: <SELECT NAME="sex"> <BR>END_OF_HTML <BR> if ($any) { <BR> print <<END_OF_HTML; <BR><OPTION VALUE="">Doesn't Matter <BR>END_OF_HTML <BR> } <BR> print <<END_OF_HTML; <BR><OPTION VALUE="1">Male <BR><OPTION VALUE="2">Female <BR></select><br> <BR><p> <BR>Enrolled in:<br> <BR>END_OF_HTML <BR> &print_subjects("MULTIPLE SIZE=5"); <BR> <BR>} <BR>By using three parameters, this function customizes a form template so that it can be used for several very different purposes. Notice that this helper function calls another helper function, print_subjects. This function queries the subject table as <BR>seen in the Msql.pm example and prints out a list of all of the available subjects. <BR> <BR>sub print_subjects { <BR> my $modifier = ""; <BR> $modifier = shift if @_; <BR> print qq%<SELECT NAME="subjects" $modifier>\n%; <BR> my $out = $dbh->query("select * from subject order by name"); <BR> while(my(%keys)=$out->fetchhash) { <BR> print qq%<OPTION VALUE="$keys{'id'}">$keys{'name'}\n%; <BR> } <BR> print "</select>\n"; <BR>} <BR>The search parameters entered in this first form are then sent to the search2 function, which actually performs the search. This is actually the function written to search for a student to view. Since its function is exactly what we need, we can <BR>piggy-back off of it as long as we tell it that we want to go to the next change function, change2, after the search. That is why we have the hidden variable subaction=change2 in the form. It tells search2, as shown in the following, where to send the <BR>user next: <BR> <BR>sub search2 { <BR> my $out = $dbh->query(&make_search_query); <BR> my $hits = $out->numrows; <BR> my $subaction = "view"; <BR> $subaction = param('subaction') if param('subaction'); <BR> print header, start_html('title'=>'Student Search Result', <BR> 'BGCOLOR'=>'white'); <BR> <BR> if (not $hits) { <BR> print <<END_OF_HTML; <BR><H1>No students found</h1> <BR><p> <BR>No students matched your criteria. <BR>END_OF_HTML <BR> } else { <BR> print <<END_OF_HTML; <BR><H1>$hits students found</h1> <BR><p> <BR><UL> <BR>END_OF_HTML <BR> while(my(%fields)=$out->fetchhash) { <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -