if ($dbh->ping()) { $sth = $dbh->prepare('SELECT ...'); $sth->execute(); ... } else { $dbh = DBI::connect(...); }