Why does my usage of Perl's DBI->connect fail silently?

dbi, perl

Solution

You're not seeing why the `connect` fails because you aren't doing what the DBI show you to do. The error will be in the `$DBI::errstr` variable:

$dbh = DBI->connect($data_source, $username, $password)
     or die $DBI::errstr;

Ensure you read the documentation for any functions or methods that you want to use. :)

Problem

Why does this code fail silently? How do I get it to show me exactly what the SQL error is? ``` $dbh=DBI->connect($db_name,$db_user,$db_pass); ``` I modified the code to look like this: ``` $dbh=DBI->connect($db_name,$db_user,$db_pass) or die("could not connect to db: $db_name"); ``` Which instead of allowing me to use `$dbh` unassigned, it will fail as expected, but it does not tell me exactly why it's failing. The values of `$db_name` etc, are all set with valid values as far as I can see. I know the real error (the MySQL server is actually not running) but for future reference, I'd like to see the true error in case I am causing an auth failure for example.

Original source