开发者

Why does my Perl CGI complain "Can't locate Mysql.pm"?

I have two folders php and perl. They contain index.php and index.pl, respectively.

My Perl code looks like:

#!/usr/bin/perl
use Mysql;

print "Content-type: text/html\n\n"; 
print "<h2>PERL-mySQL Connect</h2>";
print "page info";
$host = "localhost";
$database = "cdcol";
$user = "root";
$password = "";
$db = Mysql->connect($host, $database, $user, $password);
$db->selectdb($database);

When i run above code (by typing http://localhost:88/perl/ in the browser), I get the following error:

Can't locate Mysql.pm in @INC (@INC contains: C:/xampp/perl/site/lib/ C:/xampp/perl/lib C:/xampp/perl/site/lib C:/xampp/apache) at C:/xampp/htdocs/perl/index.pl line 2. BEGIN failed--compilation aborted at C:/xampp/htdocs/perl/index.pl line 2.

whereas browsing to http://localhost:88/php/ works.

index.php has:

<?php
$con = mysql_connect("localhost","root","");
    if($con)
    {
        if开发者_如何学Python(mysql_select_db("cdcol", $con))
        {
            $sql="SELECT Id From products";
            if(mysql_query($sql))
            {
                $result = mysql_query($sql);
                if ($result) ...


You should use DBI in conjunction with DBD::mysql.

You should use a standard CGI processing module such as CGI::Simple.

use strict; use warnings;
use CGI::Simple;
use DBI;

my $cgi = CGI::Simple->new;
my $dsn = sprintf(
    'DBI:mysql:database=%s;host=%s',
    'cdcol', 'localhost'
);

my $dbh = DBI->connect($dsn, root => '',
    { AutoCommit => 0, RaiseError => 0 }
);

my $status = $dbh ? 'Connected' : 'Failed to connect';

print $cgi->header, <<HTML;
<!DOCTYPE HTML>
<html>
<head><title>Test</title></head>
<body>
<h1>Perl CGI Script</h1>
<p>$status</p>
</body>
</html>
HTML
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜