📄 00000000.htm
字号:
exit(0) <BR>In Example 10-2, tableshow.cgi accepts the name of a database server (default is "localhost") and the name of a database on that server. The program then shows all of the available tables on that server. <BR> <BR>Example 10-2: The CGI tableshow.cgi shows all of the tables within a database <BR>#!/usr/bin/perl -w <BR> <BR>use strict; <BR>use CGI qw(:standard); <BR>use CGI::Carp; <BR> # Use the Msql.pm module <BR>use DBI; <BR>CGI::use_named_parameters(1); <BR> <BR>my ($db); <BR>my $output = new CGI; <BR>$db = param('db') or die("Database not supplied!"); <BR> <BR># Connect to the requested server. <BR>my $dbh = DBI->connect("DBI:mysql:$db:$server", undef, undef); <BR> <BR># If $dbh does not exist, the attempt to connect to the <BR># database server failed. The server may not be running, <BR># or the given database may not exist. <BR>if (not $dbh) { <BR> print header, start_html('title'=>"Information on $host => $db", <BR> 'BGCOLOR'=>'white'); <BR> <BR> print <<END_OF_HTML; <BR><H1>$host</h1> <BR><H2>$db</h2> <BR>The connection attempt failed for the following reason:<BR> <BR>$DBI::errstr <BR></body></html> <BR>END_OF_HTML <BR> exit(0); <BR>} <BR> <BR>print header, start_html('title'=>"Information on $host => $db", <BR> 'BGCOLOR'=>'white'); <BR>print <<END_OF_HTML; <BR><H1>$host</h1> <BR><H2>$db</h2> <BR><p> <BR>Tables:<br> <BR><UL> <BR>END_OF_HTML <BR># $dbh->listtable returns an array of the tables that are available <BR># in the current database. <BR>my @tables = $dbh->func( '_ListTables' ); <BR>foreach (@tables) { <BR> print "<LI>$_\n"; <BR>} <BR>print <<END_OF_HTML; <BR></ul> <BR></body></html> <BR>END_OF_HTML <BR> exit(0); <BR>And, finally, Example 10-3 shows all of the information about a specific table. <BR> <BR>Example 10-3: The CGI tabledump.cgi shows information about a specific table <BR>#!/usr/bin/perl -w <BR> <BR>use strict; <BR>use CGI qw(:standard); <BR>use CGI::Carp; <BR> # Use the DBI module <BR>use DBI; <BR>CGI::use_named_parameters(1); <BR> <BR>my ($db,$table); <BR>my $output = new CGI; <BR>$server = param('server') or $server = ''; <BR>$db = param('db') or die("Database not supplied!"); <BR> <BR># Connect to the requested server. <BR>my $dbh = DBI->connect("DBI:mysql:$db:$server", undef, undef); <BR> <BR># We now prepare a query for the server asking for all of the data in <BR># the table. <BR>my $table_data = $dbh->prepare("select * from $table"); <BR># Now send the query to the server. <BR>$table_data->execute; <BR> <BR># If the return value is undefined, the table must not exist. (Or it could <BR># be empty; we don't check for that.) <BR>if (not $table_data) { <BR> print header, start_html('title'=> <BR> "Information on $host => $db => $table", 'BGCOLOR'=>'white'); <BR> <BR> print <<END_OF_HTML; <BR><H1>$host</h1> <BR><H2>$db</h2> <BR>The table '$table' does not exist in $db on $host. <BR></body></html> <BR>END_OF_HTML <BR> exit(0); <BR>} <BR> <BR># At this point, we know we have data to display. First we show the <BR># layout of the table. <BR>print header, start_html('title'=>"Information on $host => $db => $table", <BR> 'BGCOLOR'=>'white'); <BR>print <<END_OF_HTML; <BR><H1>$host</h1> <BR><H2>$db</h2> <BR><H3>$table</h3> <BR><p> <BR><TABLE BORDER> <BR><CAPTION>Fields</caption> <BR><TR> <BR> <TH>Field<TH>Type<TH>Size<TH>NOT NULL <BR></tr> <BR><UL> <BR>END_OF_HTML <BR> <BR># $table_data->name returns a referece to an array <BR># of the fields of the database. <BR>my @fields = @{$table_data->NAME}; <BR># $table_data->type return an array reference of the types of fields. <BR># The types returned here are in SQL standard notation, not MySQL specific. <BR>my @types = @{$table_data->TYPE}; <BR># $table_data->is_not_null returns a Boolean array ref indicating which fields <BR># have the 'NOT NULL' flag. <BR>my @not_null = @{$table_data->is_not_null}; <BR># $table_data->length return an array ref of the lengths of the fields. This is <BR># fixed for INT and REAL types, but variable (defined when the table was <BR># created) for CHAR. <BR>my @length = @{$table_data->length}; <BR> <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -