Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to raise exception on database errors #1257

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Shadow243 marked this conversation as resolved.
Show resolved Hide resolved
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);
Shadow243 marked this conversation as resolved.
Show resolved Hide resolved
Hm_Debug::add(sprintf('Connecting to dsn: %s', $dsn));
return self::$dbh[$key];
} catch (Exception $oops) {
Expand Down