1

I just learned poo and i got to play with perl, achieved this but I do not get the expected output, problem with mysql? Or bad code?. other thing, the same query runs on console and workbench, and this module add 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;

this file called method and created object. add to chmod +x file.pl , but i don't know, whats not work?

#!/usr/bin/perl

use MysqlTest;
use warnings;
use strict;

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

output in console

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.

any idea?

opmeitle
  • 195
  • 6
  • 19

1 Answers1

3

The OO itself is correct.

The error message comes from MySQL, denying access for the user 'delkav', but I the user you want to connect with is 'root'.

Anyway, seems your DBI->connect() line is wrong. To follow the DBD::mysql docs, you must change your line:

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

to

my $connect = DBI->connect("DBI:mysql:database=$database;host=$hostname;", $user, $pwd) or die $DBI::errstr;
Cafe
  • 104
  • 1
  • 3
  • solve it but changing the mysql root pass, now the problem is at line 31 -> Can't call method "prepare" on an undefined value at MysqlTest.pm line 31. -> our $result = $connect->prepare($id); ??? and for you code before lines.. – opmeitle Sep 29 '12 at 16:17
  • Use of uninitialized value $database in concatenation (.) or string at MysqlTest.pm line 18. Use of uninitialized value $hostname in concatenation (.) or string at MysqlTest.pm line 18. Can't call method "prepare" on an undefined value at MysqlTest.pm line 28. – opmeitle Sep 29 '12 at 16:42