Skip to content

Commit ef942d6

Browse files
committed
Merge pull request #183 from alexfacciorusso/master
Added “has” helper method, fixed closing php tag.
2 parents 9026a39 + a1f9cad commit ef942d6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

MysqliDb.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function get($tableName, $numRows = null, $columns = '*')
267267
$columns = '*';
268268

269269
$column = is_array($columns) ? implode(', ', $columns) : $columns;
270-
$this->_query = "SELECT $column FROM " .self::$_prefix . $tableName;
270+
$this->_query = "SELECT $column FROM " . self::$_prefix . $tableName;
271271
$stmt = $this->_buildQuery($numRows);
272272

273273
if ($this->isSubQuery)
@@ -345,6 +345,20 @@ public function insert($tableName, $insertData)
345345
return true;
346346
}
347347

348+
/**
349+
* A convenient function that returns TRUE if exists at least an element that
350+
* satisfy the where condition specified calling the "where" method before this one.
351+
*
352+
* @param string $tableName The name of the database table to work with.
353+
*
354+
* @return array Contains the returned rows from the select query.
355+
*/
356+
public function has($tableName)
357+
{
358+
$this->getOne($tableName, '1');
359+
return $this->count >= 1;
360+
}
361+
348362
/**
349363
* Update query. Be sure to first call the "where" method.
350364
*
@@ -1124,3 +1138,4 @@ public function _transaction_status_check () {
11241138
$this->rollback ();
11251139
}
11261140
} // END class
1141+
?>

readme.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ 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)**
17+
**[EXISTS / NOT EXISTS condition](#exists--not-exists-condition)**
18+
**[Has method](#has-method)**
1819
**[Helper Functions](#helper-commands)**
1920
**[Transaction Helpers](#transaction-helpers)**
2021

@@ -386,6 +387,18 @@ $products = $db->get ("products");
386387
// Gives SELECT * FROM products WHERE EXISTS (select userId from users where company='testCompany')
387388
```
388389

390+
### Has method
391+
A convenient function that returns TRUE if exists at least an element that satisfy the where condition specified calling the "where" method before this one.
392+
```php
393+
$db->where("user", $user);
394+
$db->where("password", md5($password));
395+
if($db->has("users")) {
396+
return "You are logged";
397+
} else {
398+
return "Wrong user/password";
399+
}
400+
```
401+
389402
### Helper commands
390403
Reconnect in case mysql connection died
391404
```php

0 commit comments

Comments
 (0)