add some changes
[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 /**
10 * PDO object
11 * @var object
12 */
13 private $conn = NULL;
14
15 /**
16 * error string
17 * @var string
18 */
19 private $error = '';
20
21 /**
22 * Connects to SQL Server
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
32 */
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);
40 try {
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
51 $this->conn = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $options);
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 }
62
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";
80 return false;
81 }
82 }
83 else{
84 $this->error = "not supported database type found";
85 return false;
86 }
87
88 return true;
89
90 }
91 catch (PDOException $e) {
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 *
107 * @param string $res
108 * @param array $bind
109 * @return integer
110 */
111 public function query ($res, $bind = array()) {
112 try {
113 $query = Null;
114 $query = $this->conn->prepare($res);
115
116 if (is_array($bind) && !empty($bind)) {
117 $query->execute($bind);
118 }
119 else {
120 $query->execute();
121 }
122
123 return $query;
124 }
125 catch (PDOException $e) {
126 $this->error = $e->getMessage();
127 }
128 }
129
130 /**
131 * Gets a row from SQL database query result.
132 *
133 * @param string $res
134 * @return array
135 */
136 public function fetch_array ($res) {
137 try {
138 return $res->fetch(PDO::FETCH_ASSOC);
139 }
140 catch (PDOException $e) {
141 $this->error = $e->getMessage();
142 }
143 }
144
145 /**
146 * return the last insert id
147 *
148 * @return integer
149 */
150 public function last_id () {
151 return $this->conn->lastInsertId();
152 }
153
154 /**
155 * Returns SQL last error.
156 *
157 * @return string
158 */
159 public function error () {
160 return $this->error;
161 }
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 }
177 }
178 ?>