perl数据库句柄始终未定义

perl数据库句柄始终未定义,perl,cgi,dbi,Perl,Cgi,Dbi,我在使用perl进行简单的DBI连接时遇到问题 我放置了一个位置检查器,以查看数据库句柄是否未定义,以及它是否总是未定义。有什么明显的错误吗?我确信我输入的信息是正确的,因为我正在php脚本中使用它 #!/usr/bin/perl -w use warnings; print "Content-Type: text/html\n\n"; use DBI; # Connecting to the database # Replace DATABASENAME with the name of

我在使用perl进行简单的DBI连接时遇到问题

我放置了一个位置检查器,以查看数据库句柄是否未定义,以及它是否总是未定义。有什么明显的错误吗?我确信我输入的信息是正确的,因为我正在php脚本中使用它

#!/usr/bin/perl -w
use warnings;
print "Content-Type: text/html\n\n";

use DBI;

# Connecting to the database
# Replace DATABASENAME with the name of the database,
# HOSTNAME with the hostname/ip address of the MySQL server.
$drh = DBI->install_driver("mysql");
$dsn =   "DBI:mysql:database=db_name;host=host_name";
$dbh = DBI->connect($dsn,"username","password");
if(defined $dbh)
{
    print "Yes<br />";
}
else
{
    print "No<br />";
 }

 # Select the data and display to the browser

   $sth = $dbh->prepare("SELECT * FROM customer");
   $sth->execute();
   while (my $ref = $sth->fetchrow_hashref()) {
   print "Found a row: id = $ref->{'cid'}";
}

$sth->finish();

# Disconnect from the database.

$dbh->disconnect();
如果connect返回undef,则发生错误。在那里抛出一些错误处理。 它可能会给你一些有用的信息

$dbh = DBI->connect($dsn, $user, $pw)
   or die "Unable to connect: $DBI::errstr\n";
如果connect返回undef,则发生错误。在那里抛出一些错误处理。 它可能会给你一些有用的信息

$dbh = DBI->connect($dsn, $user, $pw)
   or die "Unable to connect: $DBI::errstr\n";

如果使用{RaiseError=>1}连接会发生什么?$drh=DBI->install_drivermysql;如果使用{RaiseError=>1}连接会发生什么?$drh=DBI->install_drivermysql;当我在代码中添加以下内容或死亡时,它是否会在之后不打印语句?我感觉perl有些问题,但不知道从哪里开始$dbh=DBI->connect$dsn、username、pw或die无法连接:$DBI::errstr\n;打印原因;问题不太可能出在perl上。您可以手动连接到数据库吗?您发送的所有参数都正确吗?也在代码中添加“use strict”。我在我的计算机上运行了您的代码,并添加了“die”语句,但收到一个错误。use strict帮助确定了问题。事实证明,它是在评估数据库句柄中的部分变量,并使这些单引号阻止了这一点!谢谢伟大的您应该始终使用“严格使用”。这会帮你省去很多麻烦。当我在代码中添加以下内容或死亡时,之后它不会打印语句吗?我感觉perl有些问题,但不知道从哪里开始$dbh=DBI->connect$dsn、username、pw或die无法连接:$DBI::errstr\n;打印原因;问题不太可能出在perl上。您可以手动连接到数据库吗?您发送的所有参数都正确吗?也在代码中添加“use strict”。我在我的计算机上运行了您的代码,并添加了“die”语句,但收到一个错误。use strict帮助确定了问题。事实证明,它是在评估数据库句柄中的部分变量,并使这些单引号阻止了这一点!谢谢伟大的您应该始终使用“严格使用”。这会帮你省去很多麻烦。