【问题标题】:Cannot display data from database无法显示数据库中的数据
【发布时间】:2017-04-18 09:54:35
【问题描述】:

我正在编写一个 Perl 程序,将姓名和年龄信息插入 MySQL 表并显示该表的当前内容。

这是 XHTML 数据

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head>
    <title>Perl Database Interfaces</title> 
  </head> 
  <body> 
    <form action="/cgi-bin/10.pl" method="post">
      <p> <b>Enter Your Information</b> <br /> 
        Name: <input type="text" name="name" /> <br />
        Age: <input type="text" name="age" /> <br /> 
        <input type="submit" value="Add" /> 
        <input type="reset" value="Clear" />
      </p> 
    </form> 
  </body>
</html>  

这里是 Perl 代码

#!/usr/bin/perl

use CGI ':standard';

print "content-type:text/html\n\n";

use DBI;

$dbh  = DBI->connect( "DBI:mysql:Temp", "root", "" );
$name = param( "name" );
$age  = param( "age" );

$sql  = "insert into Students values ('$name','$age')";
$sth  = $dbh->prepare( "$sql" );
$sth->execute;

$sql = "select * from Students";
$sth = $dbh->prepare( $sql );
$sth->execute;

print "<table border size=1>  <tr> 
<th>Name</th> 
<th>Age</th> </tr> ";

while ( ( $name, $age ) = $sth->fetchrow() ) {
    print "<tr> 
    <td>$name</td> 
    <td>$age</td>
     </tr> ";
}

$sth->finish();

$dbh->disconnect();

print "</table>";

数据正在插入数据库(我使用终端手动检查),但数据未显示在 Web 浏览器上。显示表格标题,但不显示行。查看我得到的输出

我做错了什么?

【问题讨论】:

  • 这段代码有很多问题,但您要问的具体问题是语句句柄没有名为fetchrow的方法。
  • 过去 10 年编写的任何 Perl 代码都需要在顶部有 use strictuse warnings
  • 好的。你能告诉我可以用什么代替 fetchrow() 吗?
  • fetchrow_array? fetchrow_arrayref? fetchrow_hashref? fetchall_arrayref?它们都在 DBI 文档中,如果您费心阅读它就会知道!

标签: mysql perl xhtml


【解决方案1】:

代码

use strict;
use warnings;

use CGI qw(:standard);
use DBI;

my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:", undef, undef, {
    RaiseError => 1,
    PrintError => 0,
});

$dbh->do(q{
    create table students (
        name varchar(255),
        age int
    )
});

my $name = param("name");
my $age  = param("age");
$dbh->do(q{insert into students values (?, ?)}, undef, $name, $age);

print header();
print <<'EOF';
<table border='1'>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
EOF

my $sth = $dbh->prepare(q{select name, age from students});
$sth->execute;

while (($name, $age) = $sth->fetchrow_array) {
    print <<EOF;
  <tr>
    <td>$name</td>
    <td>$age</td>
  </tr>
EOF
}

print qq~</table>\n~;

命令行

$ perl 40949974.pl 'name=John Smith&age=30'

输出

Content-Type: text/html; charset=ISO-8859-1

<table border='1'>
  <tr>
    <th>Name</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>John Smith</td>
    <td>30</td>
  </tr>
</table>

说明

通过使用内存中的 SQLite 数据库并通过命令行提供 CGI 参数,我们有一个完整的、自包含的示例。对于插入,您实际上并不需要 prepare/execute 循环,除非您要插入一堆记录(例如,在循环中)。我还为插入使用了占位符和绑定值,它为您正确地转义了您的变量。但是,如果您通过网页接受用户输入,您还需要进行额外的验证。

您可以阅读DBI documentation 以了解有关这些概念的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 2015-06-26
    • 2019-02-02
    • 2016-10-05
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多