Commit cfd0a9f9 authored by Alexander Butenko's avatar Alexander Butenko

Revert "Revert "Merge pull request #5 from screeper/master""

This reverts commit 743541f7.
parent 743541f7
...@@ -19,6 +19,12 @@ class MysqliDb ...@@ -19,6 +19,12 @@ class MysqliDb
* @var MysqliDb * @var MysqliDb
*/ */
protected static $_instance; protected static $_instance;
/**
* Table prefix
*
* @var string
*/
protected $_prefix;
/** /**
* MySQLi instance * MySQLi instance
* *
...@@ -106,6 +112,7 @@ class MysqliDb ...@@ -106,6 +112,7 @@ class MysqliDb
$this->port = $port; $this->port = $port;
$this->connect(); $this->connect();
$this->setPrefix();
self::$_instance = $this; self::$_instance = $this;
} }
...@@ -149,6 +156,16 @@ class MysqliDb ...@@ -149,6 +156,16 @@ class MysqliDb
$this->_query = null; $this->_query = null;
$this->count = 0; $this->count = 0;
} }
/**
* Method to set a prefix
*
* @param string $prefix Contains a tableprefix
*/
public function setPrefix($prefix = '')
{
$this->_prefix = $prefix;
}
/** /**
* Pass in a raw query and an array containing the parameters to bind to the prepaird statement. * Pass in a raw query and an array containing the parameters to bind to the prepaird statement.
...@@ -214,7 +231,7 @@ class MysqliDb ...@@ -214,7 +231,7 @@ class MysqliDb
$columns = '*'; $columns = '*';
$column = is_array($columns) ? implode(', ', $columns) : $columns; $column = is_array($columns) ? implode(', ', $columns) : $columns;
$this->_query = "SELECT $column FROM $tableName"; $this->_query = "SELECT $column FROM $this->_prefix$tableName";
$stmt = $this->_buildQuery($numRows); $stmt = $this->_buildQuery($numRows);
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
...@@ -248,7 +265,7 @@ class MysqliDb ...@@ -248,7 +265,7 @@ class MysqliDb
*/ */
public function insert($tableName, $insertData) public function insert($tableName, $insertData)
{ {
$this->_query = "INSERT into $tableName"; $this->_query = "INSERT into $this->_prefix$tableName";
$stmt = $this->_buildQuery(null, $insertData); $stmt = $this->_buildQuery(null, $insertData);
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
...@@ -267,7 +284,7 @@ class MysqliDb ...@@ -267,7 +284,7 @@ class MysqliDb
*/ */
public function update($tableName, $tableData) public function update($tableName, $tableData)
{ {
$this->_query = "UPDATE $tableName SET "; $this->_query = "UPDATE $this->_prefix$tableName SET ";
$stmt = $this->_buildQuery(null, $tableData); $stmt = $this->_buildQuery(null, $tableData);
$stmt->execute(); $stmt->execute();
...@@ -287,7 +304,7 @@ class MysqliDb ...@@ -287,7 +304,7 @@ class MysqliDb
*/ */
public function delete($tableName, $numRows = null) public function delete($tableName, $numRows = null)
{ {
$this->_query = "DELETE FROM $tableName"; $this->_query = "DELETE FROM $this->_prefix$tableName";
$stmt = $this->_buildQuery($numRows); $stmt = $this->_buildQuery($numRows);
$stmt->execute(); $stmt->execute();
...@@ -348,7 +365,7 @@ class MysqliDb ...@@ -348,7 +365,7 @@ class MysqliDb
if ($joinType && !in_array ($joinType, $allowedTypes)) if ($joinType && !in_array ($joinType, $allowedTypes))
die ('Wrong JOIN type: '.$joinType); die ('Wrong JOIN type: '.$joinType);
$this->_join[$joinType . " JOIN " . $joinTable] = $joinCondition; $this->_join[$joinType . " JOIN " . $this->_prefix.$joinTable] = $joinCondition;
return $this; return $this;
} }
......
...@@ -10,6 +10,11 @@ After that, create a new instance of the class. ...@@ -10,6 +10,11 @@ After that, create a new instance of the class.
$db = new Mysqlidb('host', 'username', 'password', 'databaseName'); $db = new Mysqlidb('host', 'username', 'password', 'databaseName');
``` ```
It's also possible to set a table prefix:
```php
$db->setPrefix('tablePrefix');
```
Next, prepare your data, and call the necessary methods. Next, prepare your data, and call the necessary methods.
### Insert Query ### Insert Query
......
...@@ -5,6 +5,9 @@ error_reporting(E_ALL); ...@@ -5,6 +5,9 @@ error_reporting(E_ALL);
$db = new Mysqlidb('localhost', 'root', '', 'testdb'); $db = new Mysqlidb('localhost', 'root', '', 'testdb');
if(!$db) die("Database error"); if(!$db) die("Database error");
$prefix = 'prefix_';
$db->setPrefix($prefix)
$tables = Array ( $tables = Array (
'users' => Array ( 'users' => Array (
'login' => 'char(10) not null', 'login' => 'char(10) not null',
...@@ -92,7 +95,7 @@ function createTable ($name, $data) { ...@@ -92,7 +95,7 @@ function createTable ($name, $data) {
foreach ($tables as $name => $fields) { foreach ($tables as $name => $fields) {
$db->rawQuery("DROP TABLE $name"); $db->rawQuery("DROP TABLE $name");
createTable ($name, $fields); createTable ($prefix.$name, $fields);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment