From e441b5837b84f8313e506a3d1b087f269f4a9fb3 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 17 Apr 2013 21:19:00 +0400 Subject: initial --- classes/db/pdo.php | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 classes/db/pdo.php (limited to 'classes/db/pdo.php') diff --git a/classes/db/pdo.php b/classes/db/pdo.php new file mode 100644 index 000000000..aaac892c6 --- /dev/null +++ b/classes/db/pdo.php @@ -0,0 +1,87 @@ +pdo = new PDO($connstr, $user, $pass); + } catch (PDOException $e) { + die($e->getMessage()); + } + + return $this->pdo; + } + + function escape_string($s, $strip_tags = true) { + if ($strip_tags) $s = strip_tags($s); + + $qs = $this->pdo->quote($s); + + return mb_substr($qs, 1, mb_strlen($qs)-2); + } + + function query($query, $die_on_error = true) { + try { + return $this->pdo->query($query); + } catch (PDOException $e) { + user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING); + } + } + + function fetch_assoc($result) { + try { + if ($result) { + return $result->fetch(); + } else { + return null; + } + } catch (PDOException $e) { + user_error($e->getMessage(), E_USER_WARNING); + } + } + + function num_rows($result) { + try { + if ($result) { + return $result->rowCount(); + } else { + return false; + } + } catch (PDOException $e) { + user_error($e->getMessage(), E_USER_WARNING); + } + } + + function fetch_result($result, $row, $param) { + $line = $this->fetch_assoc($result); + + if ($line) + return $line[$param]; + else + return null; + + } + + function close() { + $this->pdo = null; + } + + function affected_rows($result) { + try { + if ($result) { + return $result->rowCount(); + } else { + return null; + } + } catch (PDOException $e) { + user_error($e->getMessage(), E_USER_WARNING); + } + } + + function last_error() { + return join(" ", $pdo->errorInfo()); + } +} +?> -- cgit v1.2.3