Perl print syntax not working when run on browser CGI - perl

print syntax not working after my $dbh = DBI->connect($dsn, "username", "password"); has been called. But when I put print syntax on the top of my $dbh = DBI->connect($dsn, "username", "password"); print work properly. This case happen when I run this code through browser using CGI and when I run this code in command-line both work properly.
Here are the code:
#!"C:\Strawberry\perl\bin\perl.exe"
use CGI qw(:standard);
use DBI;
use JSON;
print header("application/json");
my $dsn = "DBI:mysql:database=webservices;host=localhost;port=3306";
print "test"; #work properly
my $dbh = DBI->connect($dsn, "root", "bukanjombloboy");
print "test"; #not working
my $result = $dbh->prepare("SELECT * FROM news");
$result->execute();
my $json_text = to_json($result->fetchall_arrayref());
print $json_text;
$dbh->disconnect();
Sorry for my bad English, thanks anyway.

It could be a issue with the EOL, in the line:
my $dbh = DBI->connect ($dsn, "root", "bukanjombloboy");
Check all your EOL are equal in all lines.

I have found the solution.
The problem is APACHE webserver doesn't have permission to access the library so I have to run XAMPP on administrator mode.

Related

error in establishing DB connection in Perl script?

I am using perl 5.24. I am trying to learn Perl.
I had written a simple Perl code to connect to a DB. But gives error stating
DBI connect('database=vms','DBA',...) failed: (no error string) at simpleperl.pl line 13.
The code is
#!/usr/bin/perl
use DBI;
use DBD::SQLAnywhere;
my $driver = "SQLAnywhere";
my $database = "vms";
my $dsn = "DBI:$driver:database=$database";
my $userid = "DBA";
my $password = "admin";
my $dbh = DBI->connect($dsn, $userid, $password,{RaiseError => 1}) or die ("died connection:$DBI::errstr");
if($dbh)
{
print "Connection Established";
}
Can anyone point out what might be the problem here?
Note the following in DBD::SQLAnywhere documentation:
$dbh = DBI->connect( 'dbi:SQLAnywhere:ENG=demo', $userid, $passwd );
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $driver = "SQLAnywhere";
my $database = "vms";
my $dsn = "DBI:$driver:ENG=$database";
my $userid = "DBA";
my $password = "admin";
my $dbh = DBI->connect($dsn, $userid, $password, {RaiseError => 1});
print "Connection established\n";
$dbh->disconnect;
Note also the following:
Always use strict and warnings.
You do not need use DBD::SQLAnywhere;. DBI will pick the driver based on what you specify in the connection string.
You specified {RaiseError => 1} in your connection options. That means, there is no need for the or die. DBI will croak if connect fails.
You probably want AutoCommit => 0 to go with that RaiseError => 1.
There is no need for the if ($dbh) following the connection attempt. You won't get there unless connect succeeded.
Given that fixing the connection string did not solve the problem and I do not have an instance of a SQLAnywhere database to test things, I am going to recommend you add:
DBI->trace( 5 );
before the connect call, and update your question with the trace information. See also TRACING.

DBI->Connect not working with mysql driver

I have the following code
#!/usr/bin/perl -w
use DBI;
use strict;
my $user = "test3";
my $password = "test3";
my $dsn = "dbi:mysql:hospital;localhost;3306";
my $dbh = DBI->connect($dsn, $user, $password);
I am getting the error
Can't locate object method "driver" via package "DBD::mysql" at /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/DBI.pm line 821.
Perhaps the capitalisation of DBD 'mysql' isn't right. at /Users/hrai36/Documents/test2.pl line 9.
I am very new to perl. I installed DBD::mysql using cpan. I don't know what Im doing wrong.

Perl database connection

I am new in perl, and I need to connect the database use DBI. My code as follows:
use LWP::Simple;
use XML::Simple qw(:strict);
use Data::Dumper;
use DBI;
use Getopt::Long;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use IO::File;
use warnings;
$dbh = DBI->connect("dbi:);
if (!$dbh) {
&logMsg(0, "$DBI::errstr");
die;
} else {&logMsg(0,"Connection to $dbName DB OK")}
I already set the values. Its kind like connection failed, but I didn't get any errors. I also check the log file, there is nothing showing. What can I do for checking the errors? Thanks for any comments and help.
I can't find anything wrong with your code, unless logMsg just doesn't work, but it's a tedious way to go about using DBI.
Rather than checking if something went wrong with DBI, it's much better to set DBI to throw an error. You can do this with RaiseError.
my $dbh = DBI->connect(
"dbi:ODBC:DSN=$dbName;Server=$dbHost",
$dbUser, $dbPassword,
{ RaiseError => 1 }
);
Now if DBI has a failure, including trying to connect, it will throw an error and stop the program. This avoids having to check for an error every time you use the database (you'll forget).
DBI;
$dbh = DBI->connect('Your_Database_Name', 'user_id','Password');
my $sth = $dbh->prepare ("select * from Table_name");
$sth->execute();
my #row_ary = $sth->hetshrow_array;
foreach $item (#row_ary)
{
print "$item\n";
}

Cannot connect to SQLite database file using Perl CGI program

My problem is that: the outputs are different, when I run the program on the linux machine, and on a web browser of another machine.
When I run the program on the linux machine, the output is:
Content-type: text/plain
11
22
username password
But when I put the program on an Apache Server, and access it using a browser on another machine, the output is simply:
11
It is probably because the program fails to connect to the database file. As I have set all the files to mode 777, that I do not have the permission is unlikely a reason.
Anyone know what the problem is and how to fix it?
#!/usr/bin/perl -w
use DBI;
print ("Content-type: text/plain\n\n");
print "11\n";
my $dbh = DBI->connect("dbi:SQLite:dbname=4140.db","","",{RaiseError => 1},) or die $DBI::errstr;
print "22\n";
my $sth = $dbh -> prepare("SELECT * FROM Credential");
$sth -> execute();
($usrname, $password) = $sth -> fetchrow();
$sth -> finish();
$dbh->disconnect();
print "$usrname $password\n";
The die strings are sent to STDERR and so won't appear in the HTTP message that is sent. You can solve this several ways, one of the simplest being to write an error handler for DBI errors that prints the error message to STDOUT.
You should also always use strict and use warnings. That way Perl will highlight many simple errors that you could otherwise easily overlook. use warnings is far superior to -w on the command line.
Take a look at this code as an example. Note that if you enable RaiseError as well as providing an error handler then DBI will raise an exception only if your error handler returns a false value.
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print ("Content-type: text/plain\n\n");
print "11\n";
my $dbh = DBI->connect('dbi:SQLite:dbname=4140.db','','',
{RaiseError => 1, PrintError => 0, HandleError => \&handle_error});
print "22\n";
my $sth = $dbh->prepare('SELECT * FROM Credential');
$sth->execute;
my ($usrname, $password) = $sth -> fetchrow();
print "$usrname $password\n";
sub handle_error {
my ($msg, $dbh, $rv) = #_;
print "DB Error: $msg\n";
0;
}
Yo should specify the complete path to your database file in order to avoid this kind of problems. Try this (if your database is at the same path as your script):
use FindBin '$Bin';
my $dbfile = "$Bin/4140.db";
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",{RaiseError => 1},) or die $DBI::errstr;
#...
Check your error log. You'll surely find that SQLite is failing to create 4140.db because of a permission error. You've made incorrect assumptions about the current directory.

Perl CGI::Session using CGI::Session::Driver::mysql

I'm having an issue with storing sessions in a MySQL database using CGI::Session.
Here is a snippet
#!/usr/bin/perl
use CGI;
use CGI::Session;
use CGI::Session::Driver::mysql;
use DBI;
use DBD::mysql;
use Net::LDAPS;
require '../include/include.pl';
$LDAP_SERVER = 'my.test.ldap.example.com';
$LDAP_SSL_PORT = '636';
$LDAP_BASE = 'ou=users,dc=example,dc=com';
$ldap = Net::LDAPS->new($LDAP_SERVER, port=> $LDAP_SSL_PORT)
or die "Unable to create LDAP object because: $! \n";
$dbh = DBI->connect("DBI:mysql:host=$db_host;database=$db_name",$db_user,$db_pswd)
or die "Unable to connect to database: \"$DBI::errstr\" $! \n";
$q = CGI->new;
$usr = $q->param('usr') || undef;
$userDN = "uid=$usr,$LDAP_BASE";
if($usr) {
$pwd = $q->param('pwd');
$ldapMsg = $ldap->bind($userDN, password=>$pwd);
$result = $ldap->code;
if ($result == 0) {
$session = CGI::Session->new('driver:mysql', undef,
{ TableName=>'car_sessions',
IdColName=>'id',
DataColName=>'a_session',
Handle=>$dbh})
or die "Unable to create session because: $!";
$session->expire('+1h');
$session->param(-name=>'car_login', -value=>$usr);
$sess_cookie = $q->cookie(-name=>'CGISESSID', -value=>$session->id, -expires=>'+1h', -path=>'/hr_car/');
$login_cookie = $q->cookie(-name=>'car_login', -value=>$usr, -expires=>'+1h', -path=>'/hr_car/');
print $q->header(-cookie=>[$sess_cookie, $login_cookie], -location=>'manage.cgi');
}
LDAP is binding correctly, and the cookies are being set correctly, but NOTHING is showing up in my sessions table!
What could I be doing wrong??
I believe the problem is with auto-flushing being unreliable. There's an explicit problem with DBI handles going out of scope before auto-flush happens, so call $session->flush once you're done setting the session up and after you delete it.
You may mitigate this problem by using file-scoped lexicals instead of globals for $dbh and friends, Perl might be able to clean them up in the right order and it's just a good idea.
PS Turn on strict and warnings and declare all those variables. Your problem could have just as easily been caused by a typo and you'd never have known it.