diff --git a/Db.class.php b/Db.class.php
index 7b4f8b2..58c6926 100644
--- a/Db.class.php
+++ b/Db.class.php
@@ -1,306 +1,296 @@
log = new Log();
- $this->Connect();
- $this->parameters = array();
- }
-
- /**
- * This method makes connection to the database.
- *
- * 1. Reads the database settings from a ini file.
- * 2. Puts the ini content into the settings array.
- * 3. Tries to connect to the database.
- * 4. If connection failed, exception is displayed and a log file gets created.
- */
- private function Connect()
- {
- $this->settings = parse_ini_file("settings.ini.php");
- $dsn = 'mysql:dbname=' . $this->settings["dbname"] . ';host=' . $this->settings["host"] . '';
- try {
- # Read settings from INI file, set UTF8
- $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(
- PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
- ));
-
- # We can now log any exceptions on Fatal error.
- $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-
- # Disable emulation of prepared statements, use REAL prepared statements instead.
- $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
-
- # Connection succeeded, set the boolean to true.
- $this->bConnected = true;
- }
- catch (PDOException $e) {
- # Write into log
- echo $this->ExceptionLog($e->getMessage());
- die();
- }
- }
- /*
- * You can use this little method if you want to close the PDO connection
- *
- */
- public function CloseConnection()
- {
- # Set the PDO object to null to close the connection
- # http://www.php.net/manual/en/pdo.connections.php
- $this->pdo = null;
- }
-
- /**
- * Every method which needs to execute a SQL query uses this method.
- *
- * 1. If not connected, connect to the database.
- * 2. Prepare Query.
- * 3. Parameterize Query.
- * 4. Execute Query.
- * 5. On exception : Write Exception into the log + SQL query.
- * 6. Reset the Parameters.
- */
- private function Init($query, $parameters = "")
- {
- # Connect to database
- if (!$this->bConnected) {
- $this->Connect();
- }
- try {
- # Prepare query
- $this->sQuery = $this->pdo->prepare($query);
-
- # Add parameters to the parameter array
- $this->bindMore($parameters);
-
- # Bind parameters
- if (!empty($this->parameters)) {
- foreach ($this->parameters as $param => $value) {
- if(is_int($value[1])) {
- $type = PDO::PARAM_INT;
- } else if(is_bool($value[1])) {
- $type = PDO::PARAM_BOOL;
- } else if(is_null($value[1])) {
- $type = PDO::PARAM_NULL;
- } else {
- $type = PDO::PARAM_STR;
- }
- // Add type when binding the values to the column
- $this->sQuery->bindValue($value[0], $value[1], $type);
- }
- }
-
- # Execute SQL
- $this->sQuery->execute();
- }
- catch (PDOException $e) {
- # Write into log and display Exception
- echo $this->ExceptionLog($e->getMessage(), $query);
- die();
- }
-
- # Reset the parameters
- $this->parameters = array();
- }
+# @object, The PDO object
+private $pdo;
+# @array, Database connection setting
+private $setting;
+# @object, PDO statement object
+private $sQuery;
+
+# @array, The database settings
+private $settings;
+
+# @bool , Connected to the database
+private $bConnected = false;
+
+# @object, Object for logging exceptions
+private $log;
+
+# @array, The parameters of the SQL query
+private $parameters;
- /**
- * @void
- *
- * Add the parameter to the parameter array
- * @param string $para
- * @param string $value
- */
- public function bind($para, $value)
- {
- $this->parameters[sizeof($this->parameters)] = [":" . $para , $value];
- }
- /**
- * @void
- *
- * Add more parameters to the parameter array
- * @param array $parray
- */
- public function bindMore($parray)
- {
- if (empty($this->parameters) && is_array($parray)) {
- $columns = array_keys($parray);
- foreach ($columns as $i => &$column) {
- $this->bind($column, $parray[$column]);
- }
- }
- }
- /**
- * If the SQL query contains a SELECT or SHOW statement it returns an array containing all of the result set row
- * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows
- *
- * @param string $query
- * @param array $params
- * @param int $fetchmode
- * @return mixed
- */
- public function query($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
- {
- $query = trim(str_replace("\r", " ", $query));
-
- $this->Init($query, $params);
-
- $rawStatement = explode(" ", preg_replace("/\s+|\t+|\n+/", " ", $query));
-
- # Which SQL statement is used
- $statement = strtolower($rawStatement[0]);
-
- if ($statement === 'select' || $statement === 'show') {
- return $this->sQuery->fetchAll($fetchmode);
- } elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') {
- return $this->sQuery->rowCount();
- } else {
- return NULL;
- }
- }
-
- /**
- * Returns the last inserted id.
- * @return string
- */
- public function lastInsertId()
- {
- return $this->pdo->lastInsertId();
- }
-
- /**
- * Starts the transaction
- * @return boolean, true on success or false on failure
- */
- public function beginTransaction()
- {
- return $this->pdo->beginTransaction();
- }
-
- /**
- * Execute Transaction
- * @return boolean, true on success or false on failure
- */
- public function executeTransaction()
- {
- return $this->pdo->commit();
- }
-
- /**
- * Rollback of Transaction
- * @return boolean, true on success or false on failure
- */
- public function rollBack()
- {
- return $this->pdo->rollBack();
- }
-
- /**
- * Returns an array which represents a column from the result set
- *
- * @param string $query
- * @param array $params
- * @return array
- */
- public function column($query, $params = null)
- {
- $this->Init($query, $params);
- $Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);
-
- $column = null;
-
- foreach ($Columns as $cells) {
- $column[] = $cells[0];
- }
-
- return $column;
-
- }
- /**
- * Returns an array which represents a row from the result set
- *
- * @param string $query
- * @param array $params
- * @param int $fetchmode
- * @return array
- */
- public function row($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
- {
- $this->Init($query, $params);
- $result = $this->sQuery->fetch($fetchmode);
- $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued,
- return $result;
- }
- /**
- * Returns the value of one single field/column
- *
- * @param string $query
- * @param array $params
- * @return string
- */
- public function single($query, $params = null)
- {
- $this->Init($query, $params);
- $result = $this->sQuery->fetchColumn();
- $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued
- return $result;
- }
- /**
- * Writes the log and returns the exception
- *
- * @param string $message
- * @param string $sql
- * @return string
- */
- private function ExceptionLog($message, $sql = "")
- {
- $exception = 'Unhandled Exception.
';
- $exception .= $message;
- $exception .= "
You can find the error back in the log.";
-
- if (!empty($sql)) {
- # Add the Raw SQL to the Log
- $message .= "\r\nRaw SQL : " . $sql;
- }
- # Write into log
- $this->log->write($message);
-
- return $exception;
- }
+/**
+* Default Constructor
+*
+* 1. Instantiate Log class.
+* 2. add $setting to DB class
+* 3. Connect to database.
+* 4. Creates the parameter array.
+*/
+public function __construct($setting)
+{
+
+$this->log = new Log();
+$this->setting = $setting;
+$this->Connect();
+$this->parameters = array();
+}
+
+/**
+* This method makes connection to the database.
+*
+* 1. Tries to connect to the database.
+* 2. If connection failed, exception is displayed and a log file gets created.
+*/
+private function Connect()
+{
+try {
+$this->pdo = new PDO($this->setting["connectionString"], $this->setting["username"], $this->setting["password"],$this->setting["pdo_setting"]);
+
+# We can now log any exceptions on Fatal error.
+$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+
+//# Disable emulation of prepared statements, use REAL prepared statements instead.
+//$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+
+# Connection succeeded, set the boolean to true.
+$this->bConnected = true;
+}
+catch (PDOException $e) {
+# Write into log
+echo $this->ExceptionLog($e->getMessage());
+die();
+}
+}
+/*
+* You can use this little method if you want to close the PDO connection
+*
+*/
+public function CloseConnection()
+{
+# Set the PDO object to null to close the connection
+# http://www.php.net/manual/en/pdo.connections.php
+$this->pdo = null;
+}
+
+/**
+* Every method which needs to execute a SQL query uses this method.
+*
+* 1. If not connected, connect to the database.
+* 2. Prepare Query.
+* 3. Parameterize Query.
+* 4. Execute Query.
+* 5. On exception : Write Exception into the log + SQL query.
+* 6. Reset the Parameters.
+*/
+private function Init($query, $parameters = "")
+{
+# Connect to database
+if (!$this->bConnected) {
+$this->Connect();
+}
+try {
+# Prepare query
+$this->sQuery = $this->pdo->prepare($query);
+
+# Add parameters to the parameter array
+$this->bindMore($parameters);
+
+# Bind parameters
+if (!empty($this->parameters)) {
+foreach ($this->parameters as $param => $value) {
+if(is_int($value[1])) {
+$type = PDO::PARAM_INT;
+} else if(is_bool($value[1])) {
+$type = PDO::PARAM_BOOL;
+} else if(is_null($value[1])) {
+$type = PDO::PARAM_NULL;
+} else {
+$type = PDO::PARAM_STR;
+}
+// Add type when binding the values to the column
+$this->sQuery->bindValue($value[0], $value[1], $type);
+}
+}
+
+# Execute SQL
+$this->sQuery->execute();
+}
+catch (PDOException $e) {
+# Write into log and display Exception
+echo $this->ExceptionLog($e->getMessage(), $query);
+die();
+}
+
+# Reset the parameters
+$this->parameters = array();
+}
+
+/**
+* @void
+*
+* Add the parameter to the parameter array
+* @param string $para
+* @param string $value
+*/
+public function bind($para, $value)
+{
+$this->parameters[sizeof($this->parameters)] = [":" . $para , $value];
+}
+/**
+* @void
+*
+* Add more parameters to the parameter array
+* @param array $parray
+*/
+public function bindMore($parray)
+{
+if (empty($this->parameters) && is_array($parray)) {
+$columns = array_keys($parray);
+foreach ($columns as $i => &$column) {
+$this->bind($column, $parray[$column]);
+}
+}
+}
+/**
+* If the SQL query contains a SELECT or SHOW statement it returns an array containing all of the result set row
+* If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows
+*
+* @param string $query
+* @param array $params
+* @param int $fetchmode
+* @return mixed
+*/
+public function query($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
+{
+$query = trim(str_replace("\r", " ", $query));
+
+$this->Init($query, $params);
+
+$rawStatement = explode(" ", preg_replace("/\s+|\t+|\n+/", " ", $query));
+
+# Which SQL statement is used
+$statement = strtolower($rawStatement[0]);
+
+if ($statement === 'select' || $statement === 'show') {
+return $this->sQuery->fetchAll($fetchmode);
+} elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') {
+return $this->sQuery->rowCount();
+} else {
+return NULL;
+}
+}
+
+/**
+* Returns the last inserted id.
+* @return string
+*/
+public function lastInsertId()
+{
+return $this->pdo->lastInsertId();
+}
+
+/**
+* Starts the transaction
+* @return boolean, true on success or false on failure
+*/
+public function beginTransaction()
+{
+return $this->pdo->beginTransaction();
+}
+
+/**
+* Execute Transaction
+* @return boolean, true on success or false on failure
+*/
+public function executeTransaction()
+{
+return $this->pdo->commit();
+}
+
+/**
+* Rollback of Transaction
+* @return boolean, true on success or false on failure
+*/
+public function rollBack()
+{
+return $this->pdo->rollBack();
+}
+
+/**
+* Returns an array which represents a column from the result set
+*
+* @param string $query
+* @param array $params
+* @return array
+*/
+public function column($query, $params = null)
+{
+$this->Init($query, $params);
+$Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);
+
+$column = null;
+
+foreach ($Columns as $cells) {
+$column[] = $cells[0];
+}
+
+return $column;
+
+}
+/**
+* Returns an array which represents a row from the result set
+*
+* @param string $query
+* @param array $params
+* @param int $fetchmode
+* @return array
+*/
+public function row($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
+{
+$this->Init($query, $params);
+$result = $this->sQuery->fetch($fetchmode);
+$this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued,
+return $result;
+}
+/**
+* Returns the value of one single field/column
+*
+* @param string $query
+* @param array $params
+* @return string
+*/
+public function single($query, $params = null)
+{
+$this->Init($query, $params);
+$result = $this->sQuery->fetchColumn();
+$this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued
+return $result;
+}
+/**
+* Writes the log and returns the exception
+*
+* @param string $message
+* @param string $sql
+* @return string
+*/
+private function ExceptionLog($message, $sql = "")
+{
+$exception = 'Unhandled Exception.
';
+$exception .= $message;
+$exception .= "
You can find the error back in the log.";
+
+if (!empty($sql)) {
+# Add the Raw SQL to the Log
+$message .= "\r\nRaw SQL : " . $sql;
+}
+# Write into log
+$this->log->write($message);
+
+return $exception;
+}
}
?>
diff --git a/README.md b/README.md
index 154eb1d..a938c10 100644
--- a/README.md
+++ b/README.md
@@ -1,31 +1,31 @@
-I am not maintaining this repository anymore and I would like to request you to find a better repo or make a fork of this repository!
-============================
-
PDO Database Class
============================
-A database class for PHP-MySQL which uses the PDO extension.
+A PHP PDO class for multi database Connections.
## To use the class
-#### 1. Edit the database settings in the settings.ini.php
+#### 1. Edit the database settings in the Setting.class.php
### Note if PDO is loading slow change localhost to -> 127.0.0.1 !
+### choose your database connection engine function sqlserv or mysql
```
-[SQL]
-host = 127.0.0.1
-user = root
-password =
-dbname = yourdatabase
+return [
+ 'connectionString' => "sqlsrv:Server=localhost,1433;Database=auth;ConnectionPooling=0",
+ 'username'=>"sa",
+ 'password'=>'123',
+ 'pdo_setting'=>[ PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ]
+ ]
```
-#### 2. Require the class in your project
+#### 2. Require the class in your project and Setting class
```php
"sqlsrv:Server=localhost,1433;Database=auth;ConnectionPooling=0",
+ 'username'=>"sa",
+ 'password'=>'123',
+ 'pdo_setting'=> [ PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ]
+ ];
+ }
+
+/*
+* mysql A static function to return mysql connection setting.
+*/
+public static function mysql(){
+ return [
+ 'connectionString' => "mysql:host=localhost;dbname=testdb",
+ 'username'=>"root",
+ 'password'=>'123',
+ 'pdo_setting'=> [ PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ]
+ ];
+ }
+}
diff --git a/index.php b/index.php
index a129393..8d5b844 100644
--- a/index.php
+++ b/index.php
@@ -1,8 +1,9 @@
-[SQL]
-host = localhost
-user = root
-password =
-dbname = testdb