Commit b4bd4cdc authored by Josh Campbell's avatar Josh Campbell

Merge pull request #178 from avbdr/master

Added WHERE (NOT?) EXISTS condition support,  Trim space from last error,  GetLastQuery does not show the Group by queries
parents 01e6d0bd 35bdeb5c
...@@ -759,6 +759,10 @@ class MysqliDb ...@@ -759,6 +759,10 @@ class MysqliDb
$this->_query .= " $key ? AND ? "; $this->_query .= " $key ? AND ? ";
$this->_bindParams ($val); $this->_bindParams ($val);
break; break;
case 'not exists':
case 'exists':
$this->_query.= $key . $this->_buildPair ("", $val);
break;
default: default:
$this->_query .= $this->_buildPair ($key, $val); $this->_query .= $this->_buildPair ($key, $val);
} }
...@@ -872,6 +876,7 @@ class MysqliDb ...@@ -872,6 +876,7 @@ class MysqliDb
$newStr .= substr ($str, 0, $pos) . $val; $newStr .= substr ($str, 0, $pos) . $val;
$str = substr ($str, $pos + 1); $str = substr ($str, $pos + 1);
} }
$newStr .= $str;
return $newStr; return $newStr;
} }
...@@ -890,7 +895,7 @@ class MysqliDb ...@@ -890,7 +895,7 @@ class MysqliDb
* @return string * @return string
*/ */
public function getLastError () { public function getLastError () {
return $this->_stmtError . " " . $this->_mysqli->error; return trim ($this->_stmtError . " " . $this->_mysqli->error);
} }
/** /**
......
...@@ -14,6 +14,7 @@ MysqliDb -- Simple MySQLi wrapper with prepared statements ...@@ -14,6 +14,7 @@ MysqliDb -- Simple MySQLi wrapper with prepared statements
**[Properties Sharing](#properties-sharing)** **[Properties Sharing](#properties-sharing)**
**[Joining Tables](#join-method)** **[Joining Tables](#join-method)**
**[Subqueries](#subqueries)** **[Subqueries](#subqueries)**
**[EXISTS / NOT EXISTS condition](#exists--not-exists-condition)**
**[Helper Functions](#helper-commands)** **[Helper Functions](#helper-commands)**
**[Transaction Helpers](#transaction-helpers)** **[Transaction Helpers](#transaction-helpers)**
...@@ -275,16 +276,19 @@ print_r ($products); ...@@ -275,16 +276,19 @@ print_r ($products);
### Properties sharing ### Properties sharing
Its is also possible to copy properties Its is also possible to copy properties
Simple pagination example:
```php ```php
$db->where ("agentId", 10); $db->where ("agentId", 10);
$db->where ("active", true);
$customers = $db->copy (); $customers = $db->copy ();
$res = $customers->get ("customers"); $res = $customers->get ("customers", Array (10, 10));
// SELECT * FROM customers where agentId = 10 // SELECT * FROM customers where agentId = 10 and active = 1 limit 10, 10
$db->orWhere ("agentId", 20); $res = $db->getOne ("customers", "count(id) as cnt");
$res = $db->get ("users"); echo "total records found: " . $res['cnt'];
// SELECT * FROM users where agentId = 10 or agentId = 20 // SELECT count(id) FROM users where agentId = 10 and active = 1
``` ```
### Subqueries ### Subqueries
...@@ -313,6 +317,17 @@ $data = Array ( ...@@ -313,6 +317,17 @@ $data = Array (
$id = $db->insert ("products", $data); $id = $db->insert ("products", $data);
// Gives INSERT INTO PRODUCTS (productName, userId, lastUpdated) values ("test product", (SELECT name FROM users WHERE id = 6), NOW()); // Gives INSERT INTO PRODUCTS (productName, userId, lastUpdated) values ("test product", (SELECT name FROM users WHERE id = 6), NOW());
``` ```
###EXISTS / NOT EXISTS condition
```php
$sub = $db->subQuery();
$sub->where("company", 'testCompany');
$sub->get ("users", null, 'userId');
$db->where (null, $sub, 'exists');
$products = $db->get ("products");
// Gives SELECT * FROM products WHERE EXISTS (select userId from users where company='testCompany')
```
### Helper commands ### Helper commands
Reconnect in case mysql connection died Reconnect in case mysql connection died
```php ```php
......
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