From 05c2463ef39eb70bf5c472c862d22d85ea1e1c29 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Fri, 27 Jun 2014 11:40:55 +0300 Subject: [PATCH 1/8] Avoid late php api usage --- MysqliDb.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index fc188bd5..7c8e57e5 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -619,7 +619,9 @@ protected function _buildQuery($numRows = null, $tableData = null) //Prepair the where portion of the query $this->_query .= ' WHERE '; $i = 0; - foreach ($this->_where as list($concat, $wValue, $wKey)) { + foreach ($this->_where as $cond) { + list ($concat, $wValue, $wKey) = $cond; + // if its not a first condition insert its concatenator (AND or OR) if ($i++ != 0) $this->_query .= " $concat "; From 1088c23fd14aea983bedef4efcccea1e8c345ef8 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 28 Jun 2014 14:59:49 +0300 Subject: [PATCH 2/8] Readme update --- readme.md | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 64749db8..3587d703 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,22 @@ -To utilize this class, first import Mysqldbi.php into your project, and require it. +### Contents +Initialization +Insert Query +Update Query +Select Query +Delete Query +Generic Query Method +Raw Query Method +Where Method +Ordering method +Grouping method +JOIN method +Properties sharing +Subqueries +Helper commands +Transaction helpers + +### Initialization +To utilize this class, first import MysqliDb.php into your project, and require it. ```php require_once('Mysqlidb.php'); @@ -122,9 +140,10 @@ print_r($results); // contains Array of returned rows ### Where Method -This method allows you to specify the parameters of the query. +This method allows you to specify where parameters of the query. +WARNING: In order to use column to column comparisons only raw where conditions should be used as column name or functions cant be passed as a bind variable. -Regular == operator: +Regular == operator with variables: ```php $db->where('id', 1); $db->where('login', 'admin'); @@ -132,6 +151,16 @@ $results = $db->get('users'); // Gives: SELECT * FROM users WHERE id=1 AND login='admin'; ``` +Regular == operator with column to column comparison: +```php +// WRONG +$db->where('lastLogin', 'createdAt'); +// CORRECT +$db->where('lastLogin = createdAt'); +$results = $db->get('users'); +// Gives: SELECT * FROM users WHERE lastLogin = createdAt; +``` + ```php $db->where('id', 50, ">="); // or $db->where('id', Array('>=' => 50)); From ac9767b3e4548e1541b709ddf13d8e7dcba78ac2 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 28 Jun 2014 15:09:20 +0300 Subject: [PATCH 3/8] refine readme --- readme.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/readme.md b/readme.md index 3587d703..09f80da5 100644 --- a/readme.md +++ b/readme.md @@ -1,19 +1,19 @@ ### Contents -Initialization -Insert Query -Update Query -Select Query -Delete Query -Generic Query Method -Raw Query Method -Where Method -Ordering method -Grouping method -JOIN method -Properties sharing -Subqueries -Helper commands -Transaction helpers +**[Initialization](#initialization)** +**[Insert Query](#insert-query)** +**[Update Query](#update-query)** +**[Select Query](#select-query)** +**[Delete Query](#delete-query)** +**[Generic Query](#generic-query-method)** +**[Raw Query](#raw-query-method)** +**[Where Conditions](#where-method)** +**[Order Conditions](#ordering-method)** +**[Group Conditions](#grouping-method)** +**[Properties Sharing](#properties-sharing)** +**[Joining Tables](#join-method)** +**[Subqueries](#subqueries)** +**[Helper Functions](#helper-commands)** +**[Transaction Helpers](#transaction-helpers)** ### Initialization To utilize this class, first import MysqliDb.php into your project, and require it. From 1188a7fbcc99bba76cb54fe89a4b96d32060f359 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 28 Jun 2014 15:10:53 +0300 Subject: [PATCH 4/8] Same --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 09f80da5..154e46c7 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -### Contents +### Table of Contents **[Initialization](#initialization)** **[Insert Query](#insert-query)** **[Update Query](#update-query)** From 87ef46b9cf16767a54b819289517ceffbd4d9787 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 28 Jun 2014 15:17:50 +0300 Subject: [PATCH 5/8] same --- readme.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/readme.md b/readme.md index 154e46c7..fd1c39f3 100644 --- a/readme.md +++ b/readme.md @@ -1,36 +1,36 @@ ### Table of Contents -**[Initialization](#initialization)** -**[Insert Query](#insert-query)** -**[Update Query](#update-query)** -**[Select Query](#select-query)** -**[Delete Query](#delete-query)** -**[Generic Query](#generic-query-method)** -**[Raw Query](#raw-query-method)** -**[Where Conditions](#where-method)** -**[Order Conditions](#ordering-method)** -**[Group Conditions](#grouping-method)** -**[Properties Sharing](#properties-sharing)** -**[Joining Tables](#join-method)** -**[Subqueries](#subqueries)** -**[Helper Functions](#helper-commands)** -**[Transaction Helpers](#transaction-helpers)** +**[Initialization](#initialization)** +**[Insert Query](#insert-query)** +**[Update Query](#update-query)** +**[Select Query](#select-query)** +**[Delete Query](#delete-query)** +**[Generic Query](#generic-query-method)** +**[Raw Query](#raw-query-method)** +**[Where Conditions](#where-method)** +**[Order Conditions](#ordering-method)** +**[Group Conditions](#grouping-method)** +**[Properties Sharing](#properties-sharing)** +**[Joining Tables](#join-method)** +**[Subqueries](#subqueries)** +**[Helper Functions](#helper-commands)** +**[Transaction Helpers](#transaction-helpers)** ### Initialization To utilize this class, first import MysqliDb.php into your project, and require it. ```php -require_once('Mysqlidb.php'); +require_once ('MysqliDb.php'); ``` After that, create a new instance of the class. ```php -$db = new Mysqlidb('host', 'username', 'password', 'databaseName'); +$db = new MysqliDb ('host', 'username', 'password', 'databaseName'); ``` Its also possible to set a table prefix: ```php -$db->setPrefix('tablePrefix'); +$db->setPrefix ('my_'); ``` Next, prepare your data, and call the necessary methods. From 0815d1dee975be3060f48bfbf1e60d611695639f Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 28 Jun 2014 15:19:49 +0300 Subject: [PATCH 6/8] Same --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index fd1c39f3..7a09a6c2 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,5 @@ +MysqliDb -- Simple MySQLi wrapper with prepared statements +
### Table of Contents **[Initialization](#initialization)** **[Insert Query](#insert-query)** From 3d1d38e7857583ac4ef0a947af83f149d254c73a Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 28 Jun 2014 15:28:33 +0300 Subject: [PATCH 7/8] same --- readme.md | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/readme.md b/readme.md index 7a09a6c2..0d4a5256 100644 --- a/readme.md +++ b/readme.md @@ -65,9 +65,9 @@ $data = Array( // Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear ); -$id = $db->insert('users', $data); -if($id) - echo 'user was created. Id='.$id; +$id = $db->insert ('users', $data); +if ($id) + echo 'user was created. Id=' . $id; ``` ### Update Query @@ -80,8 +80,8 @@ $data = Array ( 'active' => $db->not() // active = !active; ); -$db->where('id', 1); -if($db->update('users', $data)) echo 'successfully updated'; +$db->where ('id', 1); +if($db->update ('users', $data)) echo 'successfully updated'; ``` ### Select Query @@ -95,10 +95,7 @@ $users = $db->get('users', 10); //contains an Array 10 users or select with custom columns set. Functions also could be used ```php -$stats = $db->getOne ("users", "sum(id), count(*) as cnt"); -echo "total ".$stats['cnt']. "users found"; - -$cols = Array ("id, name, email"); +$cols = Array ("id", "name", "email"); $users = $db->get ("users", null, $cols); if ($db->count > 0) foreach ($users as $user) { @@ -112,6 +109,9 @@ or select just one row $db->where ("id", 1); $user = $db->getOne ("users"); echo $user['id']; + +$stats = $db->getOne ("users", "sum(id), count(*) as cnt"); +echo "total ".$stats['cnt']. "users found"; ``` ### Delete Query @@ -147,34 +147,33 @@ WARNING: In order to use column to column comparisons only raw where conditions Regular == operator with variables: ```php -$db->where('id', 1); -$db->where('login', 'admin'); -$results = $db->get('users'); +$db->where ('id', 1); +$db->where ('login', 'admin'); +$results = $db->get ('users'); // Gives: SELECT * FROM users WHERE id=1 AND login='admin'; ``` Regular == operator with column to column comparison: ```php // WRONG -$db->where('lastLogin', 'createdAt'); +$db->where ('lastLogin', 'createdAt'); // CORRECT -$db->where('lastLogin = createdAt'); -$results = $db->get('users'); +$db->where ('lastLogin = createdAt'); +$results = $db->get ('users'); // Gives: SELECT * FROM users WHERE lastLogin = createdAt; ``` ```php -$db->where('id', 50, ">="); -// or $db->where('id', Array('>=' => 50)); - -$results = $db->get('users'); +$db->where ('id', 50, ">="); +// or $db->where ('id', Array ('>=' => 50)); +$results = $db->get ('users'); // Gives: SELECT * FROM users WHERE id >= 50; ``` BETWEEN / NOT BETWEEN: ```php -$db->where('id', Array(4, 20), 'between'); -// or $db->where('id', Array('between' => Array(4, 20) ) ); +$db->where('id', Array (4, 20), 'BETWEEN'); +// or $db->where ('id', Array ('BETWEEN' => Array(4, 20))); $results = $db->get('users'); // Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20 @@ -183,7 +182,7 @@ $results = $db->get('users'); IN / NOT IN: ```php $db->where('id', Array(1, 5, 27, -1, 'd'), 'IN'); -// or $db->where('id', Array( 'in' => Array(1, 5, 27, -1, 'd') ) ); +// or $db->where('id', Array( 'IN' => Array(1, 5, 27, -1, 'd') ) ); $results = $db->get('users'); // Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd'); @@ -207,14 +206,16 @@ $results = $db->get("users"); Also you can use raw where conditions: ```php $db->where ("id != companyId"); +$db->where ("DATE(createdAt) = DATE(lastLogin)"); $results = $db->get("users"); ``` Or raw condition with variables: ```php -$db->where("id = ? or id = ?", Array(6,2)); +$db->where ("(id = ? or id = ?)", Array(6,2)); +$db->where ("login","mike") $res = $db->get ("users"); -// Gives: SELECT * FROM users WERE id = 2 or id = 2; +// Gives: SELECT * FROM users WHERE (id = 2 or id = 2) and login='mike'; ``` From 6714ad1df8e42af45ec6f8f6aacf075fff15ec48 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Sat, 28 Jun 2014 14:59:49 +0300 Subject: [PATCH 8/8] Readme update --- readme.md | 84 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/readme.md b/readme.md index 64749db8..0d4a5256 100644 --- a/readme.md +++ b/readme.md @@ -1,18 +1,38 @@ -To utilize this class, first import Mysqldbi.php into your project, and require it. +MysqliDb -- Simple MySQLi wrapper with prepared statements +
+### Table of Contents +**[Initialization](#initialization)** +**[Insert Query](#insert-query)** +**[Update Query](#update-query)** +**[Select Query](#select-query)** +**[Delete Query](#delete-query)** +**[Generic Query](#generic-query-method)** +**[Raw Query](#raw-query-method)** +**[Where Conditions](#where-method)** +**[Order Conditions](#ordering-method)** +**[Group Conditions](#grouping-method)** +**[Properties Sharing](#properties-sharing)** +**[Joining Tables](#join-method)** +**[Subqueries](#subqueries)** +**[Helper Functions](#helper-commands)** +**[Transaction Helpers](#transaction-helpers)** + +### Initialization +To utilize this class, first import MysqliDb.php into your project, and require it. ```php -require_once('Mysqlidb.php'); +require_once ('MysqliDb.php'); ``` After that, create a new instance of the class. ```php -$db = new Mysqlidb('host', 'username', 'password', 'databaseName'); +$db = new MysqliDb ('host', 'username', 'password', 'databaseName'); ``` Its also possible to set a table prefix: ```php -$db->setPrefix('tablePrefix'); +$db->setPrefix ('my_'); ``` Next, prepare your data, and call the necessary methods. @@ -45,9 +65,9 @@ $data = Array( // Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear ); -$id = $db->insert('users', $data); -if($id) - echo 'user was created. Id='.$id; +$id = $db->insert ('users', $data); +if ($id) + echo 'user was created. Id=' . $id; ``` ### Update Query @@ -60,8 +80,8 @@ $data = Array ( 'active' => $db->not() // active = !active; ); -$db->where('id', 1); -if($db->update('users', $data)) echo 'successfully updated'; +$db->where ('id', 1); +if($db->update ('users', $data)) echo 'successfully updated'; ``` ### Select Query @@ -75,10 +95,7 @@ $users = $db->get('users', 10); //contains an Array 10 users or select with custom columns set. Functions also could be used ```php -$stats = $db->getOne ("users", "sum(id), count(*) as cnt"); -echo "total ".$stats['cnt']. "users found"; - -$cols = Array ("id, name, email"); +$cols = Array ("id", "name", "email"); $users = $db->get ("users", null, $cols); if ($db->count > 0) foreach ($users as $user) { @@ -92,6 +109,9 @@ or select just one row $db->where ("id", 1); $user = $db->getOne ("users"); echo $user['id']; + +$stats = $db->getOne ("users", "sum(id), count(*) as cnt"); +echo "total ".$stats['cnt']. "users found"; ``` ### Delete Query @@ -122,28 +142,38 @@ print_r($results); // contains Array of returned rows ### Where Method -This method allows you to specify the parameters of the query. +This method allows you to specify where parameters of the query. +WARNING: In order to use column to column comparisons only raw where conditions should be used as column name or functions cant be passed as a bind variable. -Regular == operator: +Regular == operator with variables: ```php -$db->where('id', 1); -$db->where('login', 'admin'); -$results = $db->get('users'); +$db->where ('id', 1); +$db->where ('login', 'admin'); +$results = $db->get ('users'); // Gives: SELECT * FROM users WHERE id=1 AND login='admin'; ``` +Regular == operator with column to column comparison: ```php -$db->where('id', 50, ">="); -// or $db->where('id', Array('>=' => 50)); +// WRONG +$db->where ('lastLogin', 'createdAt'); +// CORRECT +$db->where ('lastLogin = createdAt'); +$results = $db->get ('users'); +// Gives: SELECT * FROM users WHERE lastLogin = createdAt; +``` -$results = $db->get('users'); +```php +$db->where ('id', 50, ">="); +// or $db->where ('id', Array ('>=' => 50)); +$results = $db->get ('users'); // Gives: SELECT * FROM users WHERE id >= 50; ``` BETWEEN / NOT BETWEEN: ```php -$db->where('id', Array(4, 20), 'between'); -// or $db->where('id', Array('between' => Array(4, 20) ) ); +$db->where('id', Array (4, 20), 'BETWEEN'); +// or $db->where ('id', Array ('BETWEEN' => Array(4, 20))); $results = $db->get('users'); // Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20 @@ -152,7 +182,7 @@ $results = $db->get('users'); IN / NOT IN: ```php $db->where('id', Array(1, 5, 27, -1, 'd'), 'IN'); -// or $db->where('id', Array( 'in' => Array(1, 5, 27, -1, 'd') ) ); +// or $db->where('id', Array( 'IN' => Array(1, 5, 27, -1, 'd') ) ); $results = $db->get('users'); // Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd'); @@ -176,14 +206,16 @@ $results = $db->get("users"); Also you can use raw where conditions: ```php $db->where ("id != companyId"); +$db->where ("DATE(createdAt) = DATE(lastLogin)"); $results = $db->get("users"); ``` Or raw condition with variables: ```php -$db->where("id = ? or id = ?", Array(6,2)); +$db->where ("(id = ? or id = ?)", Array(6,2)); +$db->where ("login","mike") $res = $db->get ("users"); -// Gives: SELECT * FROM users WERE id = 2 or id = 2; +// Gives: SELECT * FROM users WHERE (id = 2 or id = 2) and login='mike'; ```