我在使用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();
Run Code Online (Sandbox Code Playgroud)
如果connect返回undef则发生错误.在那里抛出一些错误处理.它可能会为您提供一些有用的信息.
$dbh = DBI->connect($dsn, $user, $pw)
or die "Unable to connect: $DBI::errstr\n";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
220 次 |
| 最近记录: |