Skip to content

Commit b4bd4cd

Browse files
committed
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
2 parents 01e6d0b + 35bdeb5 commit b4bd4cd

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

MysqliDb.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,10 @@ protected function _buildWhere () {
759759
$this->_query .= " $key ? AND ? ";
760760
$this->_bindParams ($val);
761761
break;
762+
case 'not exists':
763+
case 'exists':
764+
$this->_query.= $key . $this->_buildPair ("", $val);
765+
break;
762766
default:
763767
$this->_query .= $this->_buildPair ($key, $val);
764768
}
@@ -872,6 +876,7 @@ protected function replacePlaceHolders ($str, $vals) {
872876
$newStr .= substr ($str, 0, $pos) . $val;
873877
$str = substr ($str, $pos + 1);
874878
}
879+
$newStr .= $str;
875880
return $newStr;
876881
}
877882

@@ -890,7 +895,7 @@ public function getLastQuery () {
890895
* @return string
891896
*/
892897
public function getLastError () {
893-
return $this->_stmtError . " " . $this->_mysqli->error;
898+
return trim ($this->_stmtError . " " . $this->_mysqli->error);
894899
}
895900

896901
/**

readme.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ MysqliDb -- Simple MySQLi wrapper with prepared statements
1414
**[Properties Sharing](#properties-sharing)**
1515
**[Joining Tables](#join-method)**
1616
**[Subqueries](#subqueries)**
17+
**[EXISTS / NOT EXISTS condition](#exists--not-exists-condition)**
1718
**[Helper Functions](#helper-commands)**
1819
**[Transaction Helpers](#transaction-helpers)**
1920

@@ -275,16 +276,19 @@ print_r ($products);
275276

276277
### Properties sharing
277278
Its is also possible to copy properties
279+
280+
Simple pagination example:
278281
```php
279282
$db->where ("agentId", 10);
283+
$db->where ("active", true);
280284

281285
$customers = $db->copy ();
282-
$res = $customers->get ("customers");
283-
// SELECT * FROM customers where agentId = 10
286+
$res = $customers->get ("customers", Array (10, 10));
287+
// SELECT * FROM customers where agentId = 10 and active = 1 limit 10, 10
284288

285-
$db->orWhere ("agentId", 20);
286-
$res = $db->get ("users");
287-
// SELECT * FROM users where agentId = 10 or agentId = 20
289+
$res = $db->getOne ("customers", "count(id) as cnt");
290+
echo "total records found: " . $res['cnt'];
291+
// SELECT count(id) FROM users where agentId = 10 and active = 1
288292
```
289293

290294
### Subqueries
@@ -313,6 +317,17 @@ $data = Array (
313317
$id = $db->insert ("products", $data);
314318
// Gives INSERT INTO PRODUCTS (productName, userId, lastUpdated) values ("test product", (SELECT name FROM users WHERE id = 6), NOW());
315319
```
320+
321+
###EXISTS / NOT EXISTS condition
322+
```php
323+
$sub = $db->subQuery();
324+
$sub->where("company", 'testCompany');
325+
$sub->get ("users", null, 'userId');
326+
$db->where (null, $sub, 'exists');
327+
$products = $db->get ("products");
328+
// Gives SELECT * FROM products WHERE EXISTS (select userId from users where company='testCompany')
329+
```
330+
316331
### Helper commands
317332
Reconnect in case mysql connection died
318333
```php

0 commit comments

Comments
 (0)