add pdo database wrapper class
authorStricted <info@nexus-irc.de>
Wed, 6 Nov 2013 23:55:49 +0000 (00:55 +0100)
committerStricted <info@nexus-irc.de>
Wed, 6 Nov 2013 23:55:49 +0000 (00:55 +0100)
db.class.php [new file with mode: 0644]

diff --git a/db.class.php b/db.class.php
new file mode 100644 (file)
index 0000000..7a3fd10
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+/* db.class.php
+ * Copyright (C) 2013  Jan Altensen (Stricted)
+ * http://stricted.de/
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+if (!extension_loaded("pdo")) die("Missing <a href=\"http://www.php.net/manual/en/book.pdo.php\">PDO</a> PHP extension."); // check if extension loaded
+class DB {
+       private $conn = NULL;
+       private $error = NULL;
+
+       /**
+        * Connects to SQL Server
+        * 
+        * @return      true/false
+        */
+       public function connect($driver, $host, $user, $pass, $db, $port = Null) {
+               try {
+                       switch (strtolower($driver)) {
+                               case "mysql":
+                                       if (!extension_loaded("pdo_mysql")) die("Missing <a href=\"http://php.net/manual/de/ref.pdo-mysql.php\">pdo_mysql</a> PHP extension."); // check if extension loaded
+                                       if(empty($port)) $port=3306;
+                                       $this->conn = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$db, $user, $pass);
+                                       break;
+                               
+                               case "pgsql":
+                                       if (!extension_loaded("pdo_pgsql")) die("Missing <a href=\"http://php.net/manual/de/ref.pdo-pgsql.php\">pdo_pgsql</a> PHP extension."); // check if extension loaded
+                                       if(empty($port)) $port=5432;
+                                       $this->conn = new PDO("pgsql:host=".$host.";port=".$port.";dbname=".$db, $user, $pass);
+                                       break;
+                                       
+                               case "sqlite":
+                                       if (!extension_loaded("pdo_sqlite")) die("Missing <a href=\"http://php.net/manual/de/ref.pdo-sqlite.php\">pdo_sqlite</a> PHP extension."); // check if extension loaded 
+                                       if(!file_exists($db)) {
+                                               @touch($db);
+                                       }
+                                       if(file_exists($db) && is_readable($db) && is_writable($db)) {
+                                               $this->conn = new PDO("sqlite:".$db, $user, $pass);
+                                       } else {
+                                               $this->error = "cant crate/connect the/to sqlite database";
+                                               return false;
+                                       }
+                                       break;
+                                       
+                               default:
+                                       $this->error = "not supported database type found";
+                                       return false;
+                                       break;
+                       }
+                       return true;
+               } catch (PDOException $e) {
+                       $this->error = $e->getMessage();
+                       return false;
+               }
+       }
+       
+       /*
+        * close the database connection
+        */
+       public function close () {
+               $this->conn = NULL;
+       }
+       
+       /**
+        * Sends a database query to SQL server.
+        *
+        * @param       string          $res            a database query
+        * @param       array           $bind           
+        * @return      integer                                 id of the query result
+        */
+       public function query ($res, $bind = array()) {
+               try {
+                       $query = Null;
+                       $query = $this->conn->prepare($res);
+                       if(is_array($bind) && !empty($bind))
+                               $query->execute($bind);
+                       else
+                               $query->execute();
+                       return $query;
+               } catch (PDOException $e) {
+                       $this->error = $e->getMessage();
+               }
+       }
+
+       
+       /**
+        * Gets a row from SQL database query result.
+        *
+        * @param       string          $res            a database query
+        * @return                              array           a row from result
+        */
+       public function fetch_array ($res) {
+               try {
+                       return $res->fetch(PDO::FETCH_ASSOC);
+               } catch (PDOException $e) {
+                       $this->error = $e->getMessage();
+               }
+       }
+       
+       /**
+        * return the last insert id
+        */
+       public function last_id () {
+               return $this->conn->lastInsertId();
+       }
+       
+       /**
+        * Returns SQL error number for last error.
+        *
+        * @return      integer         MySQL error number
+        */
+       public function error () {
+               return $this->error;
+       }
+}
+?>