Boldkyky Posted December 17, 2012 Posted December 17, 2012 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.
Shinryuu Posted December 17, 2012 Posted December 17, 2012 I think you're missing a hostname parameter, which would be 'localhost'. Also database=$database is a tad silly, just use $database. Here's a straightforward guide.
Boldkyky Posted December 17, 2012 Author Posted December 17, 2012 I went ahead and took a look at the guide you suggested, Shinryuu. I haven't had the opportunity to implement the code yet because I'm not certain what the port for the database would be. I'm searching through phpMyAdmin but I can't seem to find it.
Shinryuu Posted December 17, 2012 Posted December 17, 2012 That guide included the port number, the ports for common services like MySQL are standardized across the web.
Boldkyky Posted December 17, 2012 Author Posted December 17, 2012 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.
Shinryuu Posted December 17, 2012 Posted December 17, 2012 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now