请问perl cgi 分页显示 mysql数据库
各位大侠,我用perl写了一个连接数据库的代码,现在有的数据库有1000多条,同时显示在一个网页上速度太慢,请问如何分页显示呢,每页显示100条,然后每页有链接,请高手指教
#!/usr/bin/perl
use strict;
use DBI;
use CGI;
print "Content-type:text/html\n\n";
print<<BEGIN;
<html>
<head>
<title>windows环境变量</title>
</head>
<body>
BEGIN
my $cgi=new CGI;
my $database=$cgi->param("database");
# Connect to target DB
my $dbh = DBI->connect("DBI:mysql:database=$database;host=localhost","root","4114", {'RaiseError' => 1});
# query
my $content=100;
my $page=1;
my $sqr = $dbh->prepare("SELECT * FROM alter_splicing limit $content");
$sqr->execute();
print "<table border='1' width=700>";
print "<tr bgcolor=#6699FF><td>est_id\n</td>";
print "<td>position\n</td>";
print "<td>sbjct\n</td>";
print "<td>est_numbers\n</td>";
print "<td>type\n</td>";
print "<td>geneid\n</td></tr>";
while(my $ref = $sqr->fetchrow_hashref()) {
my $query=$ref->{'est_id'};
my $sbjct=$ref->{'sbjct'};
print "<tr><td align='left'>$query\n</td>";
print "<td align='right'>$ref->{'position'}\n</td>";
print "<td align='left'><a href=info.cgi?database=$database&sbjct=$sbjct&query=$query>$sbjct\n</a></td>";
print "<td align='right'>$ref->{'est_numbers'}\n</td>";
print "<td align='left'>$ref->{'type'}\n</td>";
print "<td align='right'>$ref->{'geneid'}\n</td></tr>";
}
print "</table>";
$dbh->disconnect();
print<<END;
</body>
</html>
END
补充:可否帮忙改一下
答案:这是我的一个测试代码,你自己试下
use strict;
use CGI;
use DBI;
my $req=CGI->new();
print $req->header;
print $req->start_html;
my $page=$req->param("page") || 1;
my $dbh=DBI->connect(
"DBI:mysql:database:localhost","root","aaa",{
PrintError => 1,#出错时warn()
RaiseError => 0,#错误时并不结束执行
AutoCommit => 1,#立即提交
}
);
my $sql="SELECT * FROM alter_splicing limit ".(($page-1)*10).",20";
print $req->p("查询语句:$sql");
my $sth=$dbh->prepare($sql);
$sth->execute;
while(my @row=$sth->fetchrow_array){
print "<li>$row[0]</li>";
}
$dbh->disconnect();
};
my $prev=$page-1;
my $next=$page+1;
print $req->p(qq(<a href="?page=1">首页</a> | <a href="?page=$prev">上页</a> | <a href="?page=$next">下页</a>));
print $req->end_html;
其他:可否帮忙改一下
上一个:可以把CGI程序理解成服务器端提供的一个API吗?
下一个:求一“Form to email CGI”主机地址