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
$this->_query .= " $key ? AND ? ";
$this->_bindParams ($val);
break;
case 'not exists':
case 'exists':
$this->_query.= $key . $this->_buildPair ("", $val);
break;
default:
$this->_query .= $this->_buildPair ($key, $val);
}
......@@ -872,6 +876,7 @@ class MysqliDb
$newStr .= substr ($str, 0, $pos) . $val;
$str = substr ($str, $pos + 1);
}
$newStr .= $str;
return $newStr;
}
......@@ -890,7 +895,7 @@ class MysqliDb
* @return string
*/
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
**[Properties Sharing](#properties-sharing)**
**[Joining Tables](#join-method)**
**[Subqueries](#subqueries)**
**[EXISTS / NOT EXISTS condition](#exists--not-exists-condition)**
**[Helper Functions](#helper-commands)**
**[Transaction Helpers](#transaction-helpers)**
......@@ -275,16 +276,19 @@ print_r ($products);
### Properties sharing
Its is also possible to copy properties
Simple pagination example:
```php
$db->where ("agentId", 10);
$db->where ("active", true);
$customers = $db->copy ();
$res = $customers->get ("customers");
// SELECT * FROM customers where agentId = 10
$res = $customers->get ("customers", Array (10, 10));
// SELECT * FROM customers where agentId = 10 and active = 1 limit 10, 10
$db->orWhere ("agentId", 20);
$res = $db->get ("users");
// SELECT * FROM users where agentId = 10 or agentId = 20
$res = $db->getOne ("customers", "count(id) as cnt");
echo "total records found: " . $res['cnt'];
// SELECT count(id) FROM users where agentId = 10 and active = 1
```
### Subqueries
......@@ -313,6 +317,17 @@ $data = Array (
$id = $db->insert ("products", $data);
// 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
Reconnect in case mysql connection died
```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