我刚学了便便,我开始玩 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.
任何想法?