Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

DBI->connect problem

by Anonymous Monk
on Jan 20, 2007 at 23:05 UTC ( [id://595703]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a website which is already working. I have decided to chnage my hosting company. But i am having a problem getting my perl script working.

If you click on the link below it displays a list of all proeprties: http://www.goarealestates.com/cgi-bin/housing.pl?search=1&category=Buy&;submit=Go!!!

Within my script file, i have a statement as below which connects to the database

$dbh = db_connect($database,$user,$password);
And the function db_connect() is as below:-
sub db_connect { ($database,$user,$password) = @_; my $h = DBI->connect("DBI:mysql:$database", $user, $password) or printError("Unable to connect to $database" . $DBI::errstr); return $h; }
I have also got a db.cfg file which is as below
# script config file $database = "shafi"; $user = "shafi"; $password ="shafi"; 1;
But the present hosting provider has given an example of how to connect to the mysql database as below:-
================================================= #!/usr/bin/perl use DBI; print "Content-type:text/html\n\n"; $db_handle = DBI->connect("dbi:mysql:database=dbxxxxxxxx;host=dbxxx.on +eandone.co.uk;user=dboxxxxxxxx;password=xxxxxxxx") or die "Couldn't connect to database: $DBI::errstr\n"; $sql = "SELECT * FROM puretest"; $statement = $db_handle->prepare($sql) or die "Couldn't prepare query '$sql': $DBI::errstr\n"; $statement->execute() or die "Couldn't execute query '$sql': $DBI::errstr\n"; while ($row_ref = $statement->fetchrow_hashref()) { print "Name <b>$row_ref->{name}</b> has email address::< +b>$row_ref->{email}</b>.<br>"; } $db_handle->disconnect(); ===================================================
The DBI->connect statement is different and deosnt seem to work. Could someone tell me how my function should change to adapt to this hsoting provider.

Code tags added by Arunbear

Replies are listed 'Best First'.
Re: DBI->connect problem
by kyle (Abbot) on Jan 20, 2007 at 23:55 UTC

    I think this will work:

    sub db_connect { ($database,$user,$password) = @_; my $h = DBI->connect("dbi:mysql:database=$database;host=dbxxx.onea +ndone.co.uk", $user, $password) or printError("Unable to connect to $ +database" . $DBI::errstr); return $h; }

    You have to fill in the correct value for "dbxxx.oneandone.co.uk", but otherwise, I think that should do what your db_connect did before. See the DBI documentation for details and Writeup Formatting Tips for some help writing a question that's easy to read.

      To extend a little bit on kyle's example the first part of a DBI->connect() is referred to as the dsn or the Data Source Name. The DSN generally is used to store values that are needed to connect to a database. The DSN generally follows the same format for determining which driver to use and what database to connect too. After that any additional parameters will generally be database specific (like user and password).

      For example mysql has a specific parameter mysql_connect_timeout. For DB2 there is db2_query_timeout.

      In your case, the example that you copied looks like to be using parameters from an older version of DBD::mysql. The sample kyle submitted should work. If you have questions you should start looking in the DBI documentation and then move to the driver specific documentation.

Re: DBI->connect problem
by Joost (Canon) on Jan 20, 2007 at 23:52 UTC
Re: DBI->connect problem
by logie17 (Friar) on Jan 21, 2007 at 06:28 UTC
    It appears at first glance that you're missing the host to connect to in your DBI connect string.
    Cheers!
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://595703]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-25 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found