我刚刚学会了poo,我得玩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.
任何想法?
答案 0 :(得分:2)
OO本身是正确的。
错误消息来自MySQL,拒绝用户'delkav'的访问,但我想要连接的用户是'root'。
无论如何,似乎你的DBI-> connect()行是错误的。要关注DBD::mysql docs,您必须更改自己的行:
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;