Jump to content

[Answered] Question About Perl Dbi Module


Boldkyky

Recommended Posts

Hello again,

 

I'm having issue with the following code:

use DBI
use strict;

 my $driver = "mysql";
 my $database = "TESTDB";
 my $dsn = "DBI:$driver:database=$database";
 my $userid = "testuser";
 my $password = "test123";
 my $dbh = DBI->connect($dsn, $userid, $password )
		  or die $DBI::errstr;

 

At first I thought it was because I lacked the necessary Perl module for it, so I ended up installing Apache::DBI but when I checked the installed Perl modules I saw the standard DBI was included. I commented everything after "use DBI" but the error persists, which leads me to believe that it is something related with the lack of a module. However, all of the documentation I've read so far points to just needing DBI.

 

Does anyone know what I'm missing?

 

Once again, thank you for your time.

Link to comment
Share on other sites

Thanks for the information.

 

Apparently the cause for the error is the "or die" part of the line. If I comment it, the site will display normally. While I can remove this it feels a bit odd discarding something that can show if there are errors getting connected.

 

Here's how the code reads after I incorporated the style presented in the tutorial Shinryuu shared.

$driver = "mysql"; 
$database = "-----";
$dsn = "DBI:$driver:$database:localhost:3306";
$userid = "---";
$password = "----";
$dbh = DBI-&--#62;connect($dsn, $userid, $password ); #or die ("Unable to connect: $DBI::errstr\n");  

 

Edit to add: Oh! Wasn't certain if the port was the same or not, my apologies.

Link to comment
Share on other sites

I just looked at a tutorial using 'or die' as well, and I gotta say it makes no sense; 'or' is a boolean operator and shouldn't be used like that.. Try an if-else with an object comparison function to see if what is returned by the dbi connect call is really a database handle, it makes more sense.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...