Connecting Perl and Mysql

Status
Not open for further replies.

rbabbra

New Member
Messages
4
Reaction score
0
Points
0
Hello

I am unable to connect to mysql and have tried everything and gone through the forum. Please can you tell me what is wrong here? I have written a perl script and have a subroutine to try and test creating a table, inserting data and then reading from it. However, nothing seems to be happening. Please have a look at this subroutine below

I have tried changing the host variable to mysql-starka.x10hosting.com instead but still doesn't work. I have set up the mysql user and password through the cPanel too.

Please can you help!!!

Thanks

Raj

#!/usr/bin/perl

use DBI;
use DBD::mysql;

print "Content-type: text/html\n\n";

&dbtest();


sub dbtest {

# Config vars
$platform = "mysql";
$database = "rbabbra_execs";
$host = "localhost";
$user = "rbabbra_admin";
$pwd = "my_password";

# Data Source Name (DSN)
$dsn = "dbi:$platform:$database:$host";

# Perl DBI Connect
$dbexecs = DBI->connect($dsn, $user, $pwd) or die "Unable to connect: $DBI::errstr\n";

my $tblcreate=qq[
CREATE TABLE IF NOT EXISTS perlTest (
pkey int(11) NOT NULL auto_increment,
a int,
b int,
c int,
timeEnter timestamp(14),
PRIMARY KEY (pkey)
)];

my $insert= qq[
insert into perlTest (a,b,c)
values (1,2,3),(4,5,6),(7,8,9)];

my $select=qq[
select a,b,c from perlTest];


my $s = $dbexecs->prepare($tblcreate);
$s->execute();
$s = $dbexecs->prepare($insert);
$s->execute();

$s = $dbh->prepare($select);
$s->execute();


while(my @val = $s->fetchrow_array()) {
print " $val[0] $val[1] $val[2]\n";
++$count;
}
$s->finish();

$dbexecs->disconnect();

}
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
The server should be localhost.

I don't know perl well, but can you add some error checking in there? $dbexecs->prepare(....) and $s->execute() I would think return a value that will tell you if your query is good or not. PHP also has a function called mysql_error() that will fetch the last error from a MySQL query. Maybe perl has something similar?
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
I am unable to connect to mysql


$dbexecs = DBI->connect($dsn, $user, $pwd) or die "Unable to connect: $DBI::errstr\n";

So you are saying that you are getting an error here?
What is the error message?

I copied your code and added my own information, and it did not have an errror trying to connect, so it is not the code.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
check your DSN, I think it might be malformed. From what I remember, DSN are formed like
Code:
<driver>://<username>:<password>@<host>/<database>
Edit:
Nevermind, I had the PEAR::DB DSN in mind, not the PERL DSN.
 
Last edited:
Status
Not open for further replies.
Top