1

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();
4

1 に答える 1

6

connectundefが返されると、エラーが発生しました。そこにいくつかのエラー処理をスローします。それはあなたにいくつかの有用な情報を与えるかもしれません。

$dbh = DBI->connect($dsn, $user, $pw)
   or die "Unable to connect: $DBI::errstr\n";
于 2012-08-23T20:23:54.397 に答える