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>
22 * Connects to SQL Server
24 * @param string $driver
26 * @param string $username
27 * @param string $password
28 * @param string $database
29 * @param integer $port
30 * @param array $options
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.");
39 $driver = strtolower($driver);
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.");
51 $this->conn
= new PDO("mysql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $options);
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.");
63 $this->conn
= new PDO("pgsql:host=".$host.";port=".$port.";dbname=".$database, $username, $password, $options);
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.");
71 if (!file_exists($database)) {
75 if (file_exists($database) && is_readable($database) && is_writable($database)) {
76 $this->conn
= new PDO("sqlite:".$database, $username, $password, $options);
79 $this->error
= "cant crate/connect the/to sqlite database";
84 $this->error
= "not supported database type found";
91 catch (PDOException
$e) {
92 $this->error
= $e->getMessage();
98 * close the database connection
100 public function close () {
105 * Sends a database query to SQL server.
111 public function query ($res, $bind = array()) {
114 $query = $this->conn
->prepare($res);
116 if (is_array($bind) && !empty($bind)) {
117 $query->execute($bind);
125 catch (PDOException
$e) {
126 $this->error
= $e->getMessage();
131 * Gets a row from SQL database query result.
136 public function fetch_array ($res) {
138 return $res->fetch(PDO
::FETCH_ASSOC
);
140 catch (PDOException
$e) {
141 $this->error
= $e->getMessage();
146 * return the last insert id
150 public function last_id () {
151 return $this->conn
->lastInsertId();
155 * Returns SQL last error.
159 public function error () {
166 * @param string $name
167 * @param array $arguments
170 public function __call($name, $arguments) {
171 if (!method_exists($this->conn
, $name)) {
172 throw new Exception("unknown method '".$name."'");
175 return call_user_func_array(array($this->conn
, $name), $arguments);