| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > 软件开发

Linux环境下Perl语言对数据库的操纵

时间:2006-10-14 12:29:35  来源:Linux联盟收集  作者:
实例:同一主机下的运行r8PLinux联盟
主机配置:PIII450 128M 15GBr8PLinux联盟
操作系统:Red Hat Linux 6.1r8PLinux联盟
数据库:MySQL-3.22.29r8PLinux联盟
建议使用RPM方式安装,至少要安装下面三个包:r8PLinux联盟
MySQL-3.22.29-1.i386.rpmr8PLinux联盟
MySQL-client-3.22.29-1.i386.rpmr8PLinux联盟
MySQL-devel-3.22.29-1.i386.rpmr8PLinux联盟
WWW服务器:Apache 1.3.6 for Linuxr8PLinux联盟
Perl 解释器:version 5.005_03 built for i386-linuxr8PLinux联盟
DBI: 版本为:1.13r8PLinux联盟
Data-ShowTable: 版本为:3.3r8PLinux联盟
DBD: Msql-Mysql-modules-1.2018r8PLinux联盟
r8PLinux联盟
本人成功地在上述环境下实现了对MySQL数据库的访问。与Windows环境下的数据库不同,它不需要建立外部数据源.r8PLinux联盟
r8PLinux联盟
下面是一个应用的简单例子:r8PLinux联盟
r8PLinux联盟
为了统计用户访问我们的网站的次数,我们建立了一个数据库, 在其中建一个表,表名为:usedata, 共有三列:userno,userpass,lognum,分别代表用户名,密码,登录次数.r8PLinux联盟
r8PLinux联盟
假定我们使用test数据库,在该库中创建表usedata, 我们既可以编一个PERL程序实现,也可以在命令行方式下实现:r8PLinux联盟
r8PLinux联盟
r8PLinux联盟
r8PLinux联盟
#mysql testr8PLinux联盟
>CREATE TABLE usedata(userno CHAR(8) NOT NULL, usepass CHAR(8) NOT NULL, lognum INT);r8PLinux联盟
>exitr8PLinux联盟
共有两个htm文件,两个pl文件. r8PLinux联盟
regist.htm <html>r8PLinux联盟
<head>r8PLinux联盟
<meta http-equiv="Content-Type"r8PLinux联盟
content="text/html; charset=gb_2312-80">r8PLinux联盟
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">r8PLinux联盟
<title>注册</title>r8PLinux联盟
</head>r8PLinux联盟
<body>r8PLinux联盟
<p><center><font color=ff3380>用户在线注册</font></center>r8PLinux联盟
</p>r8PLinux联盟
<form action="/cgi-bin/database/regist.pl" method="POST">r8PLinux联盟
<table border="0"> r8PLinux联盟
<td valign="top"><font color="#400080">用户名<input type=text name='IDNO' size=4 maxlength=8></font>r8PLinux联盟
<td valign="top"><font color="#400080">密码<input type=password name='PASS' size=6 maxlength=8></font>r8PLinux联盟
</table>r8PLinux联盟
<font color="#0080C0"><input type="submit"r8PLinux联盟
value="注册"> <input type="reset" value="清除"> </font>r8PLinux联盟
</form>r8PLinux联盟
</body>r8PLinux联盟
</html> r8PLinux联盟
login.htm <html>r8PLinux联盟
<head>r8PLinux联盟
<meta http-equiv="Content-Type"r8PLinux联盟
content="text/html; charset=gb_2312-80">r8PLinux联盟
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">r8PLinux联盟
<title>登录</title>r8PLinux联盟
</head>r8PLinux联盟
<body>r8PLinux联盟
<p><center><font color=ff3380>用户在线登录</font></center>r8PLinux联盟
</p>r8PLinux联盟
<form action="/cgi-bin/database/login.pl" method="POST">r8PLinux联盟
<table border="0"> r8PLinux联盟
<td valign="top"><font color="#400080">用户名<input type=text name='IDNO' size=4 maxlength=8></font>r8PLinux联盟
<td valign="top"><font color="#400080">密码<input type=password name='PASS' size=6 maxlength=8></font>r8PLinux联盟
</table>r8PLinux联盟
<font color="#0080C0"><input type="submit"r8PLinux联盟
value="登录"> <input type="reset" value="清除"> </font>r8PLinux联盟
</form>r8PLinux联盟
</body>r8PLinux联盟
</html> r8PLinux联盟
regist.pl #!/usr/bin/perlr8PLinux联盟
# regist.plr8PLinux联盟
<p>read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});r8PLinux联盟
@pairs = split(/&/, $buffer);r8PLinux联盟
foreach $pair (@pairs) {r8PLinux联盟
($name, $value) = split(/=/, $pair);r8PLinux联盟
$value =~ tr/+/ /;r8PLinux联盟
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;r8PLinux联盟
$value =~ s/<!--(.|\n)*-->//g;r8PLinux联盟
$value=~ s/<([^>]|\n)*>//g;r8PLinux联盟
$value=~ s/>/>/g;r8PLinux联盟
$value=~ s/</</g; r8PLinux联盟
$FORM{$name} = $value;r8PLinux联盟
}r8PLinux联盟
<p>print "Content-type: text/html\n\n";r8PLinux联盟
<p>print "<HTML><HEAD><TITLE>注册</TITLE></HEAD>\n";r8PLinux联盟
<p>print "<body bgcolor=#FFFFFF>\n";r8PLinux联盟
<p>r8PLinux联盟
if ($FORM{'IDNO'} eq "")r8PLinux联盟
{r8PLinux联盟
print<<EOF;r8PLinux联盟
<center><font color=red>用户名不能为空!</font>r8PLinux联盟
返回<a href='javascript:history.go(-1);'>前页</a>修改r8PLinux联盟
</center>r8PLinux联盟
EOFr8PLinux联盟
exit(0);r8PLinux联盟
}r8PLinux联盟
<p>if ($FORM{'PASS'} eq "")r8PLinux联盟
{r8PLinux联盟
print<<EOF;r8PLinux联盟
<center><font color=red>密码不能为空!</font>r8PLinux联盟
返回<a href='javascript:history.go(-1);'>前页</a>修改r8PLinux联盟
</center>r8PLinux联盟
EOFr8PLinux联盟
exit(0);r8PLinux联盟
}r8PLinux联盟
<p>$host= shift || "";r8PLinux联盟
$test_db="test";r8PLinux联盟
$opt_user=$opt_password="";r8PLinux联盟
<p>use DBI;r8PLinux联盟
<p>$|= 1; # Autoflushr8PLinux联盟
<p>$table="usedata";r8PLinux联盟
<p>$dbh = DBI->connect("DBI:mysql:$test_db:$host",$opt_user,$opt_password) || die "Can't connect: $DBI::errstr\n";r8PLinux联盟
<p>$n1 = $FORM{'IDNO'};r8PLinux联盟
<p>$sth=$dbh->prepare("select * from usedata where userno=$n1 ") or die $dbh->errstr;r8PLinux联盟
<p>$sth->execute() or die $sth->errstr;r8PLinux联盟
<p>if (($row = $sth->fetchrow_arrayref))r8PLinux联盟
{r8PLinux联盟
$dbh->disconnect();r8PLinux联盟
print<<EOF;r8PLinux联盟
<center><font color=red>用户名已存在!</font>r8PLinux联盟
返回<a href='javascript:history.go(-1);'>前页</a>修改r8PLinux联盟
</center>r8PLinux联盟
EOFr8PLinux联盟
exit(0);r8PLinux联盟
}r8PLinux联盟
$n2 = $FORM{'PASS'};r8PLinux联盟
$n3 = 1;r8PLinux联盟
$dbh->do("insert into $table values($n1, $n2,$n3)") or die $DBI::errstr;r8PLinux联盟
<p>$dbh->disconnect();r8PLinux联盟
print<<EOF;r8PLinux联盟
<center>您已注册成功!</center>r8PLinux联盟
</BODY></HTML>r8PLinux联盟
EOFr8PLinux联盟
exit(0);r8PLinux联盟
<p>login.pl #!/usr/bin/perlr8PLinux联盟
# login.plr8PLinux联盟
<p>read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});r8PLinux联盟
@pairs = split(/&/, $buffer);r8PLinux联盟
foreach $pair (@pairs) {r8PLinux联盟
($name, $value) = split(/=/, $pair);r8PLinux联盟
$value =~ tr/+/ /;r8PLinux联盟
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;r8PLinux联盟
$value =~ s/<!--(.|\n)*-->//g;r8PLinux联盟
$value=~ s/<([^>]|\n)*>//g;r8PLinux联盟
$value=~ s/>/>/g;r8PLinux联盟
$value=~ s/</</g; r8PLinux联盟
$FORM{$name} = $value;r8PLinux联盟
}r8PLinux联盟
<p>print "Content-type: text/html\n\n";r8PLinux联盟
<p>print "<HTML><HEAD><TITLE>登录</TITLE></HEAD>\n";r8PLinux联盟
<p>print "<body bgcolor=#FFFFFF>\n";r8PLinux联盟
<p>r8PLinux联盟
if ($FORM{'IDNO'} eq "")r8PLinux联盟
{r8PLinux联盟
print<<EOF;r8PLinux联盟
<center><font color=red>用户名不能为空!</font>r8PLinux联盟
返回<a href='javascript:history.go(-1);'>前页</a>修改r8PLinux联盟
</center>r8PLinux联盟
EOFr8PLinux联盟
exit(0);r8PLinux联盟
}r8PLinux联盟
<p>if ($FORM{'PASS'} eq "")r8PLinux联盟
{r8PLinux联盟
print<<EOF;r8PLinux联盟
<center><font color=red>密码不能为空!</font>r8PLinux联盟
返回<a href='javascript:history.go(-1);'>前页</a>修改r8PLinux联盟
</center>r8PLinux联盟
EOFr8PLinux联盟
exit(0);r8PLinux联盟
}r8PLinux联盟
<p>$host= shift || "";r8PLinux联盟
$test_db="test";r8PLinux联盟
$opt_user=$opt_password="";r8PLinux联盟
use DBI;r8PLinux联盟
$|= 1; # Autoflushr8PLinux联盟
<p>$table="usedata";r8PLinux联盟
<p>$dbh = DBI->connect("DBI:mysql:$test_db:$host",$opt_user,$opt_password) || die "Can't connect: $DBI::errstr\n";r8PLinux联盟
<p>$n1 = $FORM{'IDNO'};r8PLinux联盟
$sth=$dbh->prepare("select * from usedata where userno=$n1 ") or die $dbh->errstr;r8PLinux联盟
<p>$sth->execute() or die $sth->errstr;r8PLinux联盟
if (($row = $sth->fetchrow_arrayref))r8PLinux联盟
{r8PLinux联盟
if ($row->[1] eq $FORM{'PASS'})r8PLinux联盟
{r8PLinux联盟
$NUM = $row->[2]+1;r8PLinux联盟
$sth=$dbh->prepare("UPDATE usedata SET lognum=$NUM where userno=$n1 ") or die $dbh->errstr;r8PLinux联盟
$sth->execute() or die $sth->errstr; print<<EOF;r8PLinux联盟
<center>您第 $NUM 次登录!</center>r8PLinux联盟
EOFr8PLinux联盟
}r8PLinux联盟
elser8PLinux联盟
{r8PLinux联盟
print<<EOF; r8PLinux联盟
<center><font color=red>密码不正确!</font>r8PLinux联盟
返回<a href='javascript:history.go(-1);'>前页</a>修改r8PLinux联盟
</center>r8PLinux联盟
EOFr8PLinux联盟
}r8PLinux联盟
}r8PLinux联盟
elser8PLinux联盟
{r8PLinux联盟
print<<EOF; r8PLinux联盟
<center><font color=red>用户名不正确!</font>r8PLinux联盟
返回<a href='javascript:history.go(-1);'>前页</a>修改r8PLinux联盟
</center>r8PLinux联盟
EOFr8PLinux联盟
}r8PLinux联盟
$dbh->disconnect();r8PLinux联盟
print <<EOF;r8PLinux联盟
</body></html>r8PLinux联盟
EOFr8PLinux联盟
exit(0);
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
    无相关信息
栏目更新
栏目热门