1

我正在制作一个连接到数据库“人民”的代码,从那里获取数据,我需要做的是使用一个按钮来获取单击此按钮并删除或更新的人的 ID。问题是我不知道如何使这个 i perl 因为在其他语言中我做到了。

my $q= new CGI;

print $q->header;
print $q-> start_html(
   -title   => "Main",
   -style  => {-src =>'/media/css/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet' },
   -script => [ 
        { -src=>'/media/js/jquery-1.9.1.js'},
        { -src=>'/media/js/jquery-ui-1.10.3.custom.js' }
   ]
);

print $q->start_form;
print $q->table({},
        $q->Tr(
            $q->th('Name', 'Surname', 'Age')
        ));   


# Connect to the database

## mysql user database name
my $db = "student";
## mysql database user name
my $user = "root";

## mysql database password
my $pass = "";

## user hostname : This should be "localhost" but it can be diffrent too
my $host="127.0.0.1";

## SQL query
my $query = "select Name,Surname,Age from student";

my $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass);
my $sqlQuery  = $dbh->prepare($query)
or die "Can't prepare $query: $dbh->errstr\n";

my $rv = $sqlQuery->execute
or die "can't execute the query: $sqlQuery->errstr";

while ( my ($Name, $Surname, $Age) = $sqlQuery->fetchrow_array() ) {
     print STDOUT "$Name  $Surname $Age";
        $q->button( print $q->button(
        -id       => 'leletebtn',
        -name     => 'submit_form',
        -value    => 'Delete',
        )          
    )
}

print $q->end_form;  
print $q->end_html;
4

1 回答 1

0

那里有很多教程。你必须使用 DBI:

http://oreilly.com/catalog/perldbi/chapter/ch04.html

http://www.perl.com/pub/199​​9/10/DBI.html

my $lastname = 'test';

my $dbh = DBI->connect('DBI:Oracle:people')
                or die "Couldn't connect to database: " . DBI->errstr;#connect
my $sth = $dbh->prepare('SELECT id,uid FROM people WHERE lastname = ?')
                or die "Couldn't prepare statement: " . $dbh->errstr;#prepare
$sth->execute($lastname); # Execute the query
while ( my $ref = $sth->fetchrow_hashref() ) {
   print "$$ref{'id'} \t $$ref{'uid'}\n";
}
于 2013-07-25T11:31:46.677 回答