add some changes
[Snippets.git] / db.class.php
CommitLineData
c99002ab 1<?php
a4685b95
S
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>
c99002ab
S
6 */
7
c99002ab 8class DB {
701c06d4
S
9 /**
10 * PDO object
11 * @var object
12 */
c99002ab 13 private $conn = NULL;
701c06d4
S
14
15 /**
16 * error string
17 * @var string
18 */
19 private $error = '';
c99002ab
S
20
21 /**
22 * Connects to SQL Server
701c06d4
S
23 *
24 * @param string $driver
25 * @param string $host
26 * @param string $username
27 * @param string $password
28 * @param string $database
29 * @param integer $port
30 * @param array $options
31 * @return boolean
c99002ab 32 */
701c06d4
S
33 public function connect($driver, $host, $username, $password, $database, $port = 0, $options = array()) {
34 if (!extension_loaded("pdo")) {
35 // check if extension loaded
36 die("Missing <a href=\"http://www.php.net/manual/en/book.pdo.php\">PDO</a> PHP extension.");
37 }
38
39 $driver = strtolower($driver);
c99002ab 40 try {
701c06d4
S
41 if ($driver == "mysql") {
42 if (!extension_loaded("pdo_mysql")) {
43 // check if extension loaded
44 die("Missing <a href=\"http://php.net/manual/de/ref.pdo-mysql.php\">pdo_mysql</a> PHP extension.");
45 }
46
47 if (empty($port)) {
48 $port=3306;
49 }
50
eaf06610 51 $this->conn = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $options);
701c06d4
S
52 }
53 else if ($driver == "pgsql") {
54 if (!extension_loaded("pdo_pgsql")) {
55 // check if extension loaded
56 die("Missing <a href=\"http://php.net/manual/de/ref.pdo-pgsql.php\">pdo_pgsql</a> PHP extension.");
57 }
58
59 if (empty($port)) {
60 $port=5432;
61 }
c99002ab 62
701c06d4
S
63 $this->conn = new PDO("pgsql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $options);
64 }
65 else if ($driver == "sqlite") {
66 if (!extension_loaded("pdo_sqlite")) {
67 // check if extension loaded
68 die("Missing <a href=\"http://php.net/manual/de/ref.pdo-sqlite.php\">pdo_sqlite</a> PHP extension.");
69 }
70
71 if (!file_exists($database)) {
72 @touch($database);
73 }
74
75 if (file_exists($database) && is_readable($database) && is_writable($database)) {
76 $this->conn = new PDO("sqlite:".$database, $username, $password, $options);
77 }
78 else {
79 $this->error = "cant crate/connect the/to sqlite database";
c99002ab 80 return false;
701c06d4 81 }
c99002ab 82 }
701c06d4
S
83 else{
84 $this->error = "not supported database type found";
85 return false;
86 }
87
c99002ab 88 return true;
701c06d4
S
89
90 }
91 catch (PDOException $e) {
c99002ab
S
92 $this->error = $e->getMessage();
93 return false;
94 }
95 }
96
97 /*
98 * close the database connection
99 */
100 public function close () {
101 $this->conn = NULL;
102 }
103
104 /**
105 * Sends a database query to SQL server.
106 *
701c06d4
S
107 * @param string $res
108 * @param array $bind
109 * @return integer
c99002ab
S
110 */
111 public function query ($res, $bind = array()) {
112 try {
113 $query = Null;
114 $query = $this->conn->prepare($res);
5f6347bf
S
115
116 if (is_array($bind) && !empty($bind)) {
c99002ab 117 $query->execute($bind);
5f6347bf
S
118 }
119 else {
c99002ab 120 $query->execute();
5f6347bf
S
121 }
122
c99002ab 123 return $query;
701c06d4
S
124 }
125 catch (PDOException $e) {
c99002ab
S
126 $this->error = $e->getMessage();
127 }
128 }
c99002ab
S
129
130 /**
131 * Gets a row from SQL database query result.
132 *
701c06d4
S
133 * @param string $res
134 * @return array
c99002ab
S
135 */
136 public function fetch_array ($res) {
137 try {
138 return $res->fetch(PDO::FETCH_ASSOC);
701c06d4
S
139 }
140 catch (PDOException $e) {
c99002ab
S
141 $this->error = $e->getMessage();
142 }
143 }
144
145 /**
146 * return the last insert id
701c06d4
S
147 *
148 * @return integer
c99002ab
S
149 */
150 public function last_id () {
151 return $this->conn->lastInsertId();
152 }
153
154 /**
701c06d4 155 * Returns SQL last error.
c99002ab 156 *
701c06d4 157 * @return string
c99002ab
S
158 */
159 public function error () {
160 return $this->error;
161 }
be1fac5b
S
162
163 /**
164 * call PDO methods
165 *
166 * @param string $name
167 * @param array $arguments
168 * @return mixed
169 */
170 public function __call($name, $arguments) {
171 if (!method_exists($this->conn, $name)) {
172 throw new Exception("unknown method '".$name."'");
173 }
174
175 return call_user_func_array(array($this->conn, $name), $arguments);
176 }
c99002ab
S
177}
178?>