1

我刚学了便便,我开始玩 perl,实现了这一点,但我没有得到预期的输出,mysql 有问题吗?还是坏代码?另一件事,相同的查询在控制台和工作台上运行,这个模块添加 chmod +x module.pm

#!/usr/bin/perl
use warnings;
use strict; 
use DBI;
use DBD::mysql;
package MysqlTest;

sub new{
    my $class = shift;
    my $query={};
    bless($query, $class);
}
sub conexion{
    my $self=shift;
    my($database, $host, $user, $pwd)=@_;
    my $connect = DBI->connect("DBI:mysql:$database:$host", $user, $pwd) or die $DBI::errstr;;
    $self->{"host"}="$host";
    $self->{"database"}="$database";
    $self->{"user"}="$user";
    $self->{"pass"}="$pwd";
    my $mysqlopen = 1;
return;
}
sub consulta{
    my $self=shift;
    if (!$mysqlopen) { &conexion; }
    my $id = "SELECT * FROM save_bookmarks WHERE id='123'";
    $result = $connect->prepare($id);
    $result->execute();
    my @resultado = $result->fetchrow_array();
    print "@resultado\n";
    return;
}
sub datos{
    my $self=shift;
    print "::DATOS DE ACCESO::\n";
    while (($key, $value)=each(%$self)){
        print "$key => $value\n";
    }
}
1;

该文件称为方法并创建了对象。添加到 chmod +x file.pl ,但我不知道,什么不起作用?

#!/usr/bin/perl

use MysqlTest;
use warnings;
use strict;

my $mysqltest = MysqlTest->new();
$mysqltest->conexion("bookmarks", "localhost", "root", "pass");
$mysqltest->consulta();

在控制台输出

DBI connect(':','',...) failed: Access denied for user 'delkav'@'localhost' (using password: NO) at MysqlTest.pm line 17.
Access denied for user 'delkav'@'localhost' (using password: NO) at MysqlTest.pm line 17.

任何想法?

4

1 回答 1

3

OO 本身是正确的。

错误消息来自 MySQL,拒绝用户“delkav”的访问,但我要连接的用户是“root”。

无论如何,您的 DBI->connect() 行似乎是错误的。要遵循DBD::mysql 文档,您必须更改您的行:

my $connect = DBI->connect("DBI:mysql:$database:$host", $user, $pwd) or die $DBI::errstr;

my $connect = DBI->connect("DBI:mysql:database=$database;host=$hostname;", $user, $pwd) or die $DBI::errstr;
于 2012-09-29T15:00:27.650 回答