Skip to content

Commit

Permalink
Merge pull request #1257 from josaphatim/raise-exceptions-db-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 authored Oct 3, 2024
2 parents 8839654 + 0fad0d3 commit a23f58a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,23 @@ static public function execute($dbh, $sql, $args, $type = false, $all = false) {
if (!$type) {
$type = self::execute_type($sql);
}
$sql = $dbh->prepare($sql);
if (!$sql || !$sql->execute($args)) {
try {
$sql = $dbh->prepare($sql);
if (!$sql || !$sql->execute($args)) {
return false;
}
if ($type == 'modify' || $type == 'insert') {
return $sql->rowCount();
}
if ($all) {
return $sql->fetchAll(PDO::FETCH_ASSOC);
}
return $sql->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $oops) {
Hm_Msgs::add('ERRDatabase error. Please try again.');
Hm_Debug::add($oops->getMessage());
return false;
}
if ($type == 'modify' || $type == 'insert') {
return $sql->rowCount();
}
if ($all) {
return $sql->fetchAll(PDO::FETCH_ASSOC);
}
return $sql->fetch(PDO::FETCH_ASSOC);
}

/**
Expand Down Expand Up @@ -139,6 +145,7 @@ static public function connect($site_config) {
try {
self::$dbh[$key] = new PDO($dsn, self::$config['db_user'], self::$config['db_pass']);
self::$dbh[$key]->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$dbh[$key]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Hm_Debug::add(sprintf('Connecting to dsn: %s', $dsn));
return self::$dbh[$key];
} catch (Exception $oops) {
Expand Down

0 comments on commit a23f58a

Please sign in to comment.