in reply to Re: how to put this db connection code into a function
in thread how to put this db connection code into a function

I suppose the above leaves RaiseError in the wrong state for subsequent prepare and other calls. Fixed:

sub connect { return eval { DBI->connect( "DBI:mysql:".join(';', "database=$sql_database", "host=$sql_host", "port=$sql_port", ), $username, $password, { RaiseError => 1, AutoCommit => 1, }, ) }; }; my $dbh = connect() or die("Can't connect: $DBI::errstr\n");

Replies are listed 'Best First'.
Re^3: how to put this db connection code into a function
by Anonymous Monk on Jan 05, 2011 at 00:19 UTC
    i don't really want to use the $DBI::errstr as it returns far less information than what is in the $@ variable. That has the line number of the problem plus the statement that was executed, whereas DBI::errstr just has a limited error message
      sub connect { return DBI->connect( "DBI:mysql:".join(';', "database=$sql_database", "host=$sql_host", "port=$sql_port", ), $username, $password, { RaiseError => 1, AutoCommit => 1, }, ); } my $dbh = eval { connect() } or die("Can't connect: $@");