Skip to content

Commit c77fd38

Browse files
committed
Merge pull request #124 from avbdr/master
Avoid late php api usage
2 parents 5fdf5f2 + cff0cab commit c77fd38

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

MysqliDb.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,9 @@ protected function _buildQuery($numRows = null, $tableData = null)
619619
//Prepair the where portion of the query
620620
$this->_query .= ' WHERE ';
621621
$i = 0;
622-
foreach ($this->_where as list($concat, $wValue, $wKey)) {
622+
foreach ($this->_where as $cond) {
623+
list ($concat, $wValue, $wKey) = $cond;
624+
623625
// if its not a first condition insert its concatenator (AND or OR)
624626
if ($i++ != 0)
625627
$this->_query .= " $concat ";

readme.md

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
To utilize this class, first import Mysqldbi.php into your project, and require it.
1+
MysqliDb -- Simple MySQLi wrapper with prepared statements
2+
<hr>
3+
### Table of Contents
4+
**[Initialization](#initialization)**
5+
**[Insert Query](#insert-query)**
6+
**[Update Query](#update-query)**
7+
**[Select Query](#select-query)**
8+
**[Delete Query](#delete-query)**
9+
**[Generic Query](#generic-query-method)**
10+
**[Raw Query](#raw-query-method)**
11+
**[Where Conditions](#where-method)**
12+
**[Order Conditions](#ordering-method)**
13+
**[Group Conditions](#grouping-method)**
14+
**[Properties Sharing](#properties-sharing)**
15+
**[Joining Tables](#join-method)**
16+
**[Subqueries](#subqueries)**
17+
**[Helper Functions](#helper-commands)**
18+
**[Transaction Helpers](#transaction-helpers)**
19+
20+
### Initialization
21+
To utilize this class, first import MysqliDb.php into your project, and require it.
222

323
```php
4-
require_once('Mysqlidb.php');
24+
require_once ('MysqliDb.php');
525
```
626

727
After that, create a new instance of the class.
828

929
```php
10-
$db = new Mysqlidb('host', 'username', 'password', 'databaseName');
30+
$db = new MysqliDb ('host', 'username', 'password', 'databaseName');
1131
```
1232

1333
Its also possible to set a table prefix:
1434
```php
15-
$db->setPrefix('tablePrefix');
35+
$db->setPrefix ('my_');
1636
```
1737

1838
Next, prepare your data, and call the necessary methods.
@@ -45,9 +65,9 @@ $data = Array(
4565
// Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
4666
);
4767

48-
$id = $db->insert('users', $data);
49-
if($id)
50-
echo 'user was created. Id='.$id;
68+
$id = $db->insert ('users', $data);
69+
if ($id)
70+
echo 'user was created. Id=' . $id;
5171
```
5272

5373
### Update Query
@@ -60,8 +80,8 @@ $data = Array (
6080
'active' => $db->not()
6181
// active = !active;
6282
);
63-
$db->where('id', 1);
64-
if($db->update('users', $data)) echo 'successfully updated';
83+
$db->where ('id', 1);
84+
if($db->update ('users', $data)) echo 'successfully updated';
6585
```
6686

6787
### Select Query
@@ -75,10 +95,7 @@ $users = $db->get('users', 10); //contains an Array 10 users
7595
or select with custom columns set. Functions also could be used
7696

7797
```php
78-
$stats = $db->getOne ("users", "sum(id), count(*) as cnt");
79-
echo "total ".$stats['cnt']. "users found";
80-
81-
$cols = Array ("id, name, email");
98+
$cols = Array ("id", "name", "email");
8299
$users = $db->get ("users", null, $cols);
83100
if ($db->count > 0)
84101
foreach ($users as $user) {
@@ -92,6 +109,9 @@ or select just one row
92109
$db->where ("id", 1);
93110
$user = $db->getOne ("users");
94111
echo $user['id'];
112+
113+
$stats = $db->getOne ("users", "sum(id), count(*) as cnt");
114+
echo "total ".$stats['cnt']. "users found";
95115
```
96116

97117
### Delete Query
@@ -122,28 +142,38 @@ print_r($results); // contains Array of returned rows
122142

123143

124144
### Where Method
125-
This method allows you to specify the parameters of the query.
145+
This method allows you to specify where parameters of the query.
146+
WARNING: In order to use column to column comparisons only raw where conditions should be used as column name or functions cant be passed as a bind variable.
126147

127-
Regular == operator:
148+
Regular == operator with variables:
128149
```php
129-
$db->where('id', 1);
130-
$db->where('login', 'admin');
131-
$results = $db->get('users');
150+
$db->where ('id', 1);
151+
$db->where ('login', 'admin');
152+
$results = $db->get ('users');
132153
// Gives: SELECT * FROM users WHERE id=1 AND login='admin';
133154
```
134155

156+
Regular == operator with column to column comparison:
135157
```php
136-
$db->where('id', 50, ">=");
137-
// or $db->where('id', Array('>=' => 50));
158+
// WRONG
159+
$db->where ('lastLogin', 'createdAt');
160+
// CORRECT
161+
$db->where ('lastLogin = createdAt');
162+
$results = $db->get ('users');
163+
// Gives: SELECT * FROM users WHERE lastLogin = createdAt;
164+
```
138165

139-
$results = $db->get('users');
166+
```php
167+
$db->where ('id', 50, ">=");
168+
// or $db->where ('id', Array ('>=' => 50));
169+
$results = $db->get ('users');
140170
// Gives: SELECT * FROM users WHERE id >= 50;
141171
```
142172

143173
BETWEEN / NOT BETWEEN:
144174
```php
145-
$db->where('id', Array(4, 20), 'between');
146-
// or $db->where('id', Array('between' => Array(4, 20) ) );
175+
$db->where('id', Array (4, 20), 'BETWEEN');
176+
// or $db->where ('id', Array ('BETWEEN' => Array(4, 20)));
147177

148178
$results = $db->get('users');
149179
// Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20
@@ -152,7 +182,7 @@ $results = $db->get('users');
152182
IN / NOT IN:
153183
```php
154184
$db->where('id', Array(1, 5, 27, -1, 'd'), 'IN');
155-
// or $db->where('id', Array( 'in' => Array(1, 5, 27, -1, 'd') ) );
185+
// or $db->where('id', Array( 'IN' => Array(1, 5, 27, -1, 'd') ) );
156186

157187
$results = $db->get('users');
158188
// Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd');
@@ -176,14 +206,16 @@ $results = $db->get("users");
176206
Also you can use raw where conditions:
177207
```php
178208
$db->where ("id != companyId");
209+
$db->where ("DATE(createdAt) = DATE(lastLogin)");
179210
$results = $db->get("users");
180211
```
181212

182213
Or raw condition with variables:
183214
```php
184-
$db->where("id = ? or id = ?", Array(6,2));
215+
$db->where ("(id = ? or id = ?)", Array(6,2));
216+
$db->where ("login","mike")
185217
$res = $db->get ("users");
186-
// Gives: SELECT * FROM users WERE id = 2 or id = 2;
218+
// Gives: SELECT * FROM users WHERE (id = 2 or id = 2) and login='mike';
187219
```
188220

189221

0 commit comments

Comments
 (0)