update copyrights
[Snippets.git] / db.class.php
1 <?php
2 /**
3 * @author Jan Altensen (Stricted)
4 * @copyright 2013-2014 Jan Altensen (Stricted)
5 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
6 */
7
8 class DB {
9 private $conn = NULL;
10 private $error = NULL;
11
12 /**
13 * Connects to SQL Server
14 *
15 * @return true/false
16 */
17 public function connect($driver, $host, $user, $pass, $db, $port = Null) {
18 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
19 try {
20 switch (strtolower($driver)) {
21 case "mysql":
22 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
23 if(empty($port)) $port=3306;
24 $this->conn = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$db, $user, $pass);
25 break;
26
27 case "pgsql":
28 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
29 if(empty($port)) $port=5432;
30 $this->conn = new PDO("pgsql:host=".$host.";port=".$port.";dbname=".$db, $user, $pass);
31 break;
32
33 case "sqlite":
34 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
35 if(!file_exists($db)) {
36 @touch($db);
37 }
38 if(file_exists($db) && is_readable($db) && is_writable($db)) {
39 $this->conn = new PDO("sqlite:".$db, $user, $pass);
40 } else {
41 $this->error = "cant crate/connect the/to sqlite database";
42 return false;
43 }
44 break;
45
46 default:
47 $this->error = "not supported database type found";
48 return false;
49 break;
50 }
51 return true;
52 } catch (PDOException $e) {
53 $this->error = $e->getMessage();
54 return false;
55 }
56 }
57
58 /*
59 * close the database connection
60 */
61 public function close () {
62 $this->conn = NULL;
63 }
64
65 /**
66 * Sends a database query to SQL server.
67 *
68 * @param string $res a database query
69 * @param array $bind
70 * @return integer id of the query result
71 */
72 public function query ($res, $bind = array()) {
73 try {
74 $query = Null;
75 $query = $this->conn->prepare($res);
76 if(is_array($bind) && !empty($bind))
77 $query->execute($bind);
78 else
79 $query->execute();
80 return $query;
81 } catch (PDOException $e) {
82 $this->error = $e->getMessage();
83 }
84 }
85
86
87 /**
88 * Gets a row from SQL database query result.
89 *
90 * @param string $res a database query
91 * @return array a row from result
92 */
93 public function fetch_array ($res) {
94 try {
95 return $res->fetch(PDO::FETCH_ASSOC);
96 } catch (PDOException $e) {
97 $this->error = $e->getMessage();
98 }
99 }
100
101 /**
102 * return the last insert id
103 */
104 public function last_id () {
105 return $this->conn->lastInsertId();
106 }
107
108 /**
109 * Returns SQL error number for last error.
110 *
111 * @return integer MySQL error number
112 */
113 public function error () {
114 return $this->error;
115 }
116 }
117 ?>