Skip to content

Commit a1f9cad

Browse files
Added “has” helper method + doc in readme.md. Fixed closing php tag.
1 parent b4bd4cd commit a1f9cad

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
@@ -250,7 +250,7 @@ public function get($tableName, $numRows = null, $columns = '*')
250250
$columns = '*';
251251

252252
$column = is_array($columns) ? implode(', ', $columns) : $columns;
253-
$this->_query = "SELECT $column FROM " .self::$_prefix . $tableName;
253+
$this->_query = "SELECT $column FROM " . self::$_prefix . $tableName;
254254
$stmt = $this->_buildQuery($numRows);
255255

256256
if ($this->isSubQuery)
@@ -304,6 +304,20 @@ public function insert($tableName, $insertData)
304304
return ($stmt->affected_rows > 0 ? $stmt->insert_id : false);
305305
}
306306

307+
/**
308+
* A convenient function that returns TRUE if exists at least an element that
309+
* satisfy the where condition specified calling the "where" method before this one.
310+
*
311+
* @param string $tableName The name of the database table to work with.
312+
*
313+
* @return array Contains the returned rows from the select query.
314+
*/
315+
public function has($tableName)
316+
{
317+
$this->getOne($tableName, '1');
318+
return $this->count >= 1;
319+
}
320+
307321
/**
308322
* Update query. Be sure to first call the "where" method.
309323
*
@@ -1058,3 +1072,4 @@ public function _transaction_status_check () {
10581072
$this->rollback ();
10591073
}
10601074
} // END class
1075+
?>

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

@@ -328,6 +329,18 @@ $products = $db->get ("products");
328329
// Gives SELECT * FROM products WHERE EXISTS (select userId from users where company='testCompany')
329330
```
330331

332+
### Has method
333+
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.
334+
```php
335+
$db->where("user", $user);
336+
$db->where("password", md5($password));
337+
if($db->has("users")) {
338+
return "You are logged";
339+
} else {
340+
return "Wrong user/password";
341+
}
342+
```
343+
331344
### Helper commands
332345
Reconnect in case mysql connection died
333346
```php

0 commit comments

Comments
 (0)