Perl + POO and Mysql error - mysql

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?

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;

Related

DBI->connect working when executed in terminal but not when executed from browser

I am writing a CGI program that prints a database query result in the browser. The script looks like this
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print "Content-type: text/html\n\n";
my $driver = "mysql";
my $database = "DBname";
my $ip = "127.0.0.1";
my $db = "DBI:$driver:DBNAME:$ip:database=$database";
my $username = "user";
my $password = "pass";
print "Connecting ...";
my $connection = DBI->connect($db, $username, $password)
or print "Couldn't connect to database: " . DBI->errstr . "\n\n";
print "Successful connection\n";
my $query = $connection->prepare("SELECT id FROM table");
$query->execute() or die $DBI::errstr;
while ( my #row = $query->fetchrow_array() ) {
my ($id) = #row;
print "ID = $id \n";
}
$query->finish();
Well, my problem is that when I run it from terminal using
perl test.cgi
it works fine, and I get the print results properly. The CGI script is located in /Library/Webserver/CGI-Executables/ and it is configured by default /cgi-bin/ in /etc/apache2/httpd.conf
If I execute it via the web browser, I just get the first print Connecting and nothing else, not even the or print from DBI->connect. I have been trying to realize what the error is, but I am unable to get a useful solution.
From your final comment it is as I suspected: the perl that the server is using simply doesn't have DBD::mysql installed, whereas your command-line perl does
Simply get that driver module installed on the server and all should be well
Note that is doesn't need to be "configured for Apache", it is simply that the copy of perl that is used by the CGI code doesn't have that module
By the way, the content type should be text/plain, not text/html: you're not generating HTML

Perl DBI Can't work out what driver to use

So I am attempting to connect to a database on an end device from one of my servers, however I'm getting the following error:
Can't connect to data source '<user>' because I can't work out what driver to use (it doesn't seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is not set) at <script> line 18
My lines of code are the following. I removed some private information of course.
my $sHDS = shift || "<host>";
my #rows;
my $cust = '<customer name>';
my $dsn = 'dbi:Sybase:' . $sHDS;
my $user = '<user>';
my $pass = '<password>';
my $hDb = DBI::connect($dsn, $user, $pass)
or die "Can not connect to ICM Database $DBI::errstr";
Anyone see where I am going wrong?
The correct call has the format
DBI->connect($dsn, $user, $password)
which is subtly but significantly different from
DBI::connect($dsn, $user, $password)
The first call is equivalent to the call
DBI::connect( 'DBI', $dsn, $user, $password )
and the connect function in DBI actually expects your dsn to be specified in the 2nd argument it receives.

unable to execute mysql query in perl while connecting to remote server

I have connected to one MySQL database which have been hosted in a remote server in Perl. Now I am trying to execute a select statement on a table in subject.pl file using Perl command line. The code is
#!/usr/bin/perl
use DBI;
use strict;
# Connected to mysql audit database in dev server
my $dsn = 'DBI:mysql:Driver={mysql}';
my $host = 'dev-mysql.learn.local';
my $database = 'subject';
my $user = 'testUser';
my $auth = 'testPassword';
my $dbh = DBI->connect("$dsn;host=$host;Database=$database",$user,$auth) or die "Database connection not made: $DBI::errstr";
# Prepare query
my $sql = "SELECT
subject_id
,subject_value
FROM
subject";
my $sth = $dbh->prepare($sql);
#Execute the statement
$sth->execute() or die "Unable to execute".$sth->errstr;
while (my #row = $sth->fetchrow_array()) {
my ($subject_id, $subject_value ) = #row;
print "$subject_id,$subject_value,$subject_db_field\n";
}
$sth->finish();
I am getting error at line $sth->execute() or die "Unable to execute".$sth->errstr;
The error message is Unable to execute at D:\Demo\perl_demo\subject.pl line 24.
But when I am printing the $dbh variable, its giving the result like DBI::db=HASH(0x1ca7884). So I guess the connection is establishing properly.
Please help me to fix this issue as I am completely new in Perl scripting.
Check for errors on prepare,
my $sth = $dbh->prepare($sql) or die $dbh->errstr;

Perl web service : Using XML RPC

Something is wrong with this code.
#!/use/bin/perl
use strict;
use warnings;
use Frontier::Daemon;
use DBI;
sub credentials {
my ($username, $password) = #_;
my $tablename = "users";
my $user = "db_user";
my $pw = "db_pass";
$dbh = DBI->connect('DBI:mysql:database;host=localhost', $user, $pw, {RaiseError => 1});
$sql = "SELECT username, password FROM $tablename";
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";
if ($sth->rows > 0) {
$login_response = "Login Successful";
} else {
$login_response = "Invalid Credentials";
return {'login' => $login_response};
die();
}
}
$methods = {'login.credentials' => \&credentials,};
Frontier::Daemon->new(LocalPort => 8080, methods => $methods)
or die "Couldn't start HTTP server: $!";
This is another problem with your code - you're not doing anything with the supplied username and password. You need to add a where clause to your SQL statement, so:
my $sql = 'SELECT * FROM users WHERE username = ? AND password = ? ';
my $sth = $dbh->prepare($sql);
$sth->execute($username, $password);
However, given that your example is selecting all records from the 'users' table, I'd have thought that credentials() would at least be returning some rows. However, I'm afraid that I've not used Frontier::Daemon in the past, so I'm not able to help on that front.
I also can't see how this code would work given that you are using strictures. $dbh, $sql, $sth and $login_response haven't been declared. So make sure that you're using 'my' in the right places - as per my example above.
To fix the problems you mentioned with returning the correct string - the logic in your if statement isn't quite right. You are returning the string 'Login Successful' when there's a successful login and the hashref { login => $login_response } when no user could be found.
I think the confusion arose from the layout of the braces. I must stress that you try and indent you code properly, which will make it much more readable to yourself and other developers when debugging and maintaining the code in the future.
The following logic should do the job.
if($sth->rows > 0){
return "Login Successful";
}
return "Invalid Credentials";

How do I call MySQL stored procedures from Perl?

How do I call MySQL stored procedures from Perl? Stored procedure functionality is fairly new to MySQL and the MySQL modules for Perl don't seem to have caught up yet.
MySQL stored procedures that produce datasets need you to use Perl DBD::mysql 4.001 or later. (http://www.perlmonks.org/?node_id=609098)
Below is a test program that will work in the newer version:
mysql> delimiter //
mysql> create procedure Foo(x int)
-> begin
-> select x*2;
-> end
-> //
perl -e 'use DBI; DBI->connect("dbi:mysql:database=bonk", "root", "")->prepare("call Foo(?)")->execute(21)'
But if you have too old a version of DBD::mysql, you get results like this:
DBD::mysql::st execute failed: PROCEDURE bonk.Foo can't return a result set in the given context at -e line 1.
You can install the newest DBD using CPAN.
There's an example in the section on Multiple result sets in the DBD::mysql docs.
#!/usr/bin/perl
# Stored Proc - Multiple Values In, Multiple Out
use strict;
use Data::Dumper;
use DBI;
my $dbh = DBI->connect('DBI:mysql:RTPC;host=db.server.com',
'user','password',{ RaiseError => 1 }) || die "$!\n";
my $sth = $dbh->prepare('CALL storedProcedure(?,?,?,?,#a,#b);');
$sth->bind_param(1, 2);
$sth->bind_param(2, 1003);
$sth->bind_param(3, 5000);
$sth->bind_param(4, 100);
$sth->execute();
my $response = $sth->fetchrow_hashref();
print Dumper $response . "\n";
It took me a while to figure it out, but I was able to get what I needed with the above. if you need to get multiple return "lines" I'm guessing you just...
while(my $response = $sth->fetchrow_hashref()) {
print Dumper $response . "\n";
}
I hope it helps.
First of all you should be probably connect through the DBI library and then you should use bind variables. E.g. something like:
#!/usr/bin/perl
#
use strict;
use DBI qw(:sql_types);
my $dbh = DBI->connect(
$ConnStr,
$User,
$Password,
{RaiseError => 1, AutoCommit => 0}
) || die "Database connection not made: $DBI::errstr";
my $sql = qq {CALL someProcedure(1);} }
my $sth = $dbh->prepare($sql);
eval {
$sth->bind_param(1, $argument, SQL_VARCHAR);
};
if ($#) {
warn "Database error: $DBI::errstr\n";
$dbh->rollback(); #just die if rollback is failing
}
$dbh->commit();
Mind you i haven't tested this, you'll have to lookup the exact syntax on CPAN.
Hi, similar to above but using SQL exec. I could not get the CALL command to work. You will need to fill in anything that is within square brackets and remove the square brackets.
use DBI;
#START: SET UP DATABASE AND CONNECT
my $host = '*[server]*\\*[database]*';
my $database = '*[table]*';
my $user = '*[user]*';
my $auth = '*[password]*';
my $dsn = "dbi:ODBC:Driver={SQL Server};Server=$host;Database=$database";
my $dbh = DBI->connect($dsn, $user, $auth, { RaiseError => 1 });
#END : SET UP DATABASE AND CONNECT
$sql = "exec *[stored procedure name]* *[param1]*,*[param2]*,*[param3]*;";
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";