From c18750007df61b6f42cf59182157afe2bcaf52ce Mon Sep 17 00:00:00 2001 From: Ruan Date: Sat, 6 Jun 2015 08:56:33 +0200 Subject: [PATCH] Use PDO instead of MySQLi --- bans.php | 4 +--- includes/page.php | 6 ++++-- includes/settings.php | 8 +++++--- mutes.php | 4 +--- warnings.php | 4 +--- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/bans.php b/bans.php index 32f7219..dab6466 100644 --- a/bans.php +++ b/bans.php @@ -36,7 +36,7 @@ fetch_assoc()) { + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { date_default_timezone_set("UTC"); $timeResult = date('F j, Y, g:i a', $row['time'] / 1000); $expiresResult = date('F j, Y, g:i a', $row['until'] / 1000); @@ -58,8 +58,6 @@ free(); - $conn->close(); ?> diff --git a/includes/page.php b/includes/page.php index 4c158dc..8c981c6 100644 --- a/includes/page.php +++ b/includes/page.php @@ -13,8 +13,10 @@ function get_query($table) { function run_query($table) { global $conn; - if (!$result = $conn->query(get_query($table))) { - die('Query error: "' . $conn->error . '"'); + try { + $result = $conn->query(get_query($table)); + } catch (PDOException $ex) { + die($ex->getMessage()); } return $result; } diff --git a/includes/settings.php b/includes/settings.php index a98de69..b3b1517 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -32,10 +32,12 @@ function litebans_connect() { global $conn, $active_query; global $table_bans, $table_mutes, $table_warnings, $table_history; - $conn = new mysqli($dbhost, $username, $password, $database); + $dsn = 'mysql:dbname=' . $database . ';host=' . $dbhost . ';charset=utf8'; - if ($conn->connect_errno > 0) { - die('Unable to connect to database: ' . $conn->connect_error); + try { + $conn = new PDO($dsn, $username, $password); + } catch (PDOException $e) { + echo 'Connection failed: ' . $e->getMessage(); } $table_bans = $table_prefix . "bans"; diff --git a/mutes.php b/mutes.php index 6779608..f63d3e4 100644 --- a/mutes.php +++ b/mutes.php @@ -26,7 +26,7 @@ fetch_assoc()) { + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { date_default_timezone_set("UTC"); $timeResult = date('F j, Y, g:i a', $row['time'] / 1000); $expiresResult = date('F j, Y, g:i a', $row['until'] / 1000); @@ -48,8 +48,6 @@ free(); - $conn->close(); ?> diff --git a/warnings.php b/warnings.php index 04667c7..ff1950d 100644 --- a/warnings.php +++ b/warnings.php @@ -33,7 +33,7 @@ fetch_assoc()) { + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { date_default_timezone_set("UTC"); $expiresResult = date('F j, Y, g:i a', $row['until'] / 1000); ?> @@ -53,8 +53,6 @@ free(); - $conn->close(); ?>