Skip to content

Commit fbf3a85

Browse files
author
Radovan Janjic
committed
Update README.md
1 parent b6fb8b8 commit fbf3a85

File tree

1 file changed

+126
-10
lines changed

1 file changed

+126
-10
lines changed

README.md

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,82 +37,102 @@ define('MySQL_DB', 'test');
3737

3838
// Example 1
3939
// Connection example
40-
///////////////////////////////////////////////////////////////////////////////////////////
40+
//
41+
4142
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
4243

4344
// Connect
4445
$db->connect();
46+
4547
// Close connection
4648
$db->close();
47-
///////////////////////////////////////////////////////////////////////////////////////////
4849

50+
51+
//
4952
// Example 2
5053
// Connection example
51-
///////////////////////////////////////////////////////////////////////////////////////////
54+
//
55+
5256
$db = new MySQL_wrapper;
5357

54-
// connect 1
58+
// Connect 1
5559
$db->connect(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
60+
5661
//
5762
// Connection 1 queries ...
5863
//
64+
5965
// Close connection 1
6066
$db->close();
6167

6268
// Connect 2
6369
$db->connect(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
70+
6471
//
6572
// Connection 2 queries ...
6673
//
74+
6775
// Close connection 2
6876
$db->close();
6977

7078
// Connect with new link
7179
$db->connect(true);
80+
7281
//
7382
// Connection 3 queries
7483
//
84+
7585
// Close connection 3
7686
$db->close();
7787

7888
// Example 3
7989
// Connection example multi host, db manipulation
8090
///////////////////////////////////////////////////////////////////////////////////////////
8191

92+
// Inst. 1
8293
$db1 = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
94+
95+
// Inst. 2
8396
$db2 = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
8497

8598
// Connect host 1
8699
$db1->connect();
100+
87101
// Connect host 2
88102
$db2->connect();
89103

90104
// Close connection host 1
91105
$db1->close();
106+
92107
// Close connection host 2
93108
$db2->close();
94109
```
95110

96111
*Select example with fetch result*
97112
```php
98113
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
114+
115+
// Connect to host
99116
$db->connect();
100117

101118
// MySQL query
102119
$db->query('SELECT * FROM `table`');
103120

104121
// Int affected rows
105-
if($db->affected){
106-
echo "<hr /><strong>Example 4 ( fetch row - array)</strong><pre>";
122+
if($db->affected > 0){
107123
while($row = $db->fetchArray()){
124+
// Result
108125
print_r($row);
109126
}
110-
echo "</pre>";
111127
}
128+
129+
// Free result memory
112130
$db->freeResult();
113131

114132
// Escape string
115133
$var = '\'';
134+
135+
// Do query
116136
$db->query("SELECT * FROM `table` WHERE `firstname` LIKE '{$db->escape($var)}';");
117137

118138
// Param to be escaped
@@ -127,17 +147,113 @@ $params['id'] = 1;
127147
$params['name'] = 'rado';
128148
$params['lname'] = 'janjic';
129149
$params['limit'] = 5;
150+
151+
// Exec query
130152
$db->query("SELECT * FROM `table` WHERE `firstname` LIKE '@name%' AND `surname` LIKE '%@lname%' OR `id` = @id LIMIT @limit;", $params);
131153

132154
// Int affected rows
133-
if($db->affected){
134-
echo "<hr /><strong>Example 4 ( fetch row - array)</strong><pre>";
155+
if($db->affected > 0){
135156
while($row = $db->fetchArray()){
157+
// Print result row
136158
print_r($row);
137159
}
138-
echo "</pre>";
139160
}
140161

162+
// Free result memory
141163
$db->freeResult();
164+
165+
// Close connection
142166
$db->close();
143167
```
168+
169+
*Prepared statements (works only with MySQLi!)*
170+
```php
171+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
172+
173+
// Works only with MySQLi!
174+
$db->extension = 'mysqli';
175+
176+
// Connect
177+
$db->connect();
178+
179+
$name = 'Radovan';
180+
181+
$stmt = $db->call('prepare', 'SELECT * FROM `table` WHERE `firstname` = ?;');
182+
$stmt->bind_param('s', $name);
183+
184+
$stmt->execute();
185+
186+
$result = $stmt->get_result();
187+
while ($row = $result->fetch_assoc()) {
188+
// do something
189+
// print_r($row);
190+
// ...
191+
}
192+
193+
// Close connection
194+
$db->close();
195+
```
196+
197+
*Prepared statements (works only with MySQLi!)*
198+
```php
199+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
200+
201+
// Connect
202+
$db->connect();
203+
204+
// Fetch query to array
205+
$array = $db->fetchQueryToArray('SELECT * FROM `table`');
206+
207+
// Print array
208+
print_r($array);
209+
210+
// Returns only first row
211+
print_r($db->fetchQueryToArray('SELECT * FROM `table`', TRUE));
212+
213+
// Close connection
214+
$db->close();
215+
```
216+
217+
*Multi results*
218+
```php
219+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
220+
221+
// Connect to host
222+
$db->connect();
223+
224+
// Result 1
225+
$r1 = $db->query('SELECT * FROM `table`');
226+
227+
// Result 2
228+
$r2 = $db->query('SELECT * FROM `table` LIMIT 2');
229+
230+
// Result 1 data
231+
if($db->numRows($r1)){
232+
while($row = $db->fetchArray($r1)){
233+
// Print rows
234+
print_r($row);
235+
}
236+
}
237+
238+
// Result 2 data
239+
if($db->numRows($r2)){
240+
while($row = $db->fetchArray($r2)){
241+
// Print rows
242+
print_r($row);
243+
}
244+
}
245+
246+
// Free relust 1
247+
$db->freeResult($r1);
248+
249+
// Free relust 2
250+
$db->freeResult($r2);
251+
252+
// Close connection
253+
$db->close();
254+
```
255+
256+
*Rows, Cols num*
257+
```php
258+
259+
```

0 commit comments

Comments
 (0)