Skip to content

Commit e2844e6

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

File tree

1 file changed

+304
-3
lines changed

1 file changed

+304
-3
lines changed

README.md

Lines changed: 304 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ It can:
2323

2424

2525
```php
26-
include "MySQL_wrapper.class.php";
27-
28-
// Set your connectivity settings here
26+
// Set your connectivity settings
2927
define('MySQL_HOST', 'localhost');
3028
define('MySQL_USER', 'root');
3129
define('MySQL_PASS', '');
@@ -255,5 +253,308 @@ $db->close();
255253

256254
*Rows, Cols num*
257255
```php
256+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
257+
258+
// Connect to host
259+
$db->connect();
260+
261+
// Do query
262+
$db->query('SELECT * FROM `table`');
263+
264+
$cols = $db->numFields();
265+
$rows = $db->numRows();
266+
267+
// ...
268+
echo "Cols: {$cols}, Rows: {$rows}";
269+
270+
// Free result memory
271+
$db->freeResult();
272+
273+
// Close connection
274+
$db->close();
275+
```
276+
277+
*Count rows*
278+
```php
279+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
280+
281+
// Connect to host
282+
$db->connect();
283+
284+
// Count all
285+
$count = $db->countRows('table');
286+
287+
// Count with condition
288+
$count2 = $db->countRows('table', "`date` = '".date("Y-m-d")."'");
289+
290+
// ...
291+
echo "Count all: {$count}, Count today: {$count2}";
292+
293+
// More info
294+
/** Retrieves the number of rows from table based on certain conditions.
295+
* @param string $table - Table name
296+
* @param string $where - WHERE Clause
297+
* @return integer or false
298+
*/
299+
// $db->countRows($table, $where = NULL)
300+
301+
// Close connection
302+
$db->close();
303+
```
304+
305+
*Array to insert*
306+
```php
307+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
258308

309+
// Connect to host
310+
$db->connect();
311+
312+
// Array data
313+
// [fealdname] = feald value
314+
$data = array();
315+
$data['firstname'] = 'Radovan';
316+
$data['surname'] = 'Janjic';
317+
$data['email'] = 'rade@it-radionica.com';
318+
// reserved values 'null', 'now()', 'curtime()', 'localtime()', 'localtime', 'utc_date()', 'utc_time()', 'utc_timestamp()'
319+
$data['date'] = 'now()';
320+
321+
// $db->arrayToInsert( ... ) returns insert id
322+
$insert_id = $db->arrayToInsert('table', $data);
323+
echo "Last insert id is: {$insert_id}";
324+
325+
// Array data
326+
// [fealdname] = feald value
327+
$data = array();
328+
$data['firstname'] = 'Radovan';
329+
$data['surname'] = 'Janjic';
330+
$data['email'] = 'rade@it-radionica.com';
331+
$data['date'] = 'now()';
332+
333+
// [fealdname] = feald value
334+
$data2 = array();
335+
$data2['firstname'] = 'Radovan';
336+
$data2['surname'] = 'Janjic';
337+
$data2['email'] = 'rade@it-radionica.com';
338+
$data2['date'] = 'now()';
339+
340+
// $db->arrayToInsert( ... ) multirow returns TRUE on success
341+
$db->arrayToInsert('table', array($data, $data2 /*, $data3 .... */ ));
342+
343+
344+
// More options
345+
/** Creates an sql string from an associate array
346+
* @param string $table - Table name
347+
* @param array $data - Data array Eg. $data['column'] = 'val';
348+
* @param boolean $ingore - INSERT IGNORE (row won't actually be inserted if it results in a duplicate key)
349+
* @param string $duplicateupdate - ON DUPLICATE KEY UPDATE (The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.)
350+
* @return insert id or false
351+
*/
352+
// $db->arrayToInsert($table, $data, $ignore = FALSE, $duplicateupdate = NULL)
353+
354+
// Close connection
355+
$db->close();
356+
```
357+
*Next AutoIncrement*
358+
```php
359+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
360+
361+
// Connect to host
362+
$db->connect();
363+
364+
// Returns next auto increment value
365+
$auto_increment = $db->nextAutoIncrement('table');
366+
367+
echo "Next auto increment id is: {$auto_increment}";
368+
369+
// Close connection
370+
$db->close();
371+
```
372+
373+
*Array to update*
374+
```php
375+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
376+
377+
// Connect to host
378+
$db->connect();
379+
380+
// Array data
381+
// [fealdname] = feald value
382+
$data = array();
383+
$data['firstname'] = 'Radovan';
384+
$data['surname'] = 'Janjic';
385+
386+
// Reserved values: null, now(), curtime(), localtime(), localtime, utc_date(), utc_time(), utc_timestamp()
387+
$data['email'] = 'null';
388+
$data['date'] = 'now()';
389+
390+
391+
$db->arrayToUpdate('table', $data, "`id` = {$insert_id}");
392+
if($db->affected > 0){
393+
echo "Updated: {$db->affected} row(s).";
394+
}
395+
396+
// Array data
397+
// [fealdname] = feald value
398+
$data = array();
399+
$data['id'] = 1; // key
400+
$data['firstname'] = 'foo';
401+
$data['surname'] = 'bar';
402+
$data['email'] = 'rade@it-radionica.com';
403+
$data['date'] = 'now()';
404+
405+
// [fealdname] = feald value
406+
$data2 = array();
407+
$data2['id'] = 2; // key
408+
$data2['firstname'] = 'Radovana';
409+
$data2['surname'] = 'Janjic';
410+
$data2['email'] = 'rade@it-radionica.com';
411+
$data2['date'] = 'now()';
412+
413+
// $db->arrayToUpdate( ... ) multirow returns TRUE on success
414+
$db->arrayToUpdate('table', array($data, $data2 /*, $data3 .... */ ));
415+
416+
// More options
417+
/** Creates an sql string from an associate array
418+
* @param string $table - Table name
419+
* @param array $data - Data array Eg. $data['column'] = 'val';
420+
* @param string $where - MySQL WHERE Clause
421+
* @param integer $limit - Limit offset
422+
* @param resource $link - link identifier
423+
* @return number of updated rows or false
424+
*/
425+
// $db->arrayToUpdate($table, $data, $where = NULL, $limit = 0, $link = 0);
426+
427+
// Close connection
428+
$db->close();
429+
```
430+
431+
*Delete row(s)*
432+
```php
433+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
434+
435+
// Connect to host
436+
$db->connect();
437+
438+
// Delete row
439+
$db->deleteRow('table', "`id` = {$insert_id}");
440+
441+
if($db->affected > 0) {
442+
echo "Deleted: {$db->affected} row(s).";
443+
}
444+
// More options
445+
/** Delete row(s) from table based on certain conditions.
446+
* @param string $table - Table name
447+
* @param string $where - WHERE Clause
448+
* @param integer $limit - Limit offset
449+
* @param resource $link - link identifier
450+
* @return number of deleted rows or false
451+
*/
452+
// $db->deleteRow($table, $where = NULL, $limit = 0, $link = 0);
453+
454+
// Close connection
455+
$db->close();
456+
```
457+
458+
*Get table columns*
459+
```php
460+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
461+
462+
// Connect
463+
$db->connect();
464+
465+
// Get table columns into array
466+
$array = $db->getColumns('table');
467+
468+
print_r($array);
469+
470+
// Close connection
471+
$db->close();
472+
```
473+
474+
*Basic Table Operation*
475+
```php
476+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
477+
478+
// Connect to host
479+
$db->connect();
480+
481+
// Copy table (with data included)
482+
$db->copyTable('table', 'table_copy');
483+
484+
// Copy table (with data included)
485+
$db->copyTable('table', 'table_copy4');
486+
487+
// Copy table structure
488+
$db->copyTable('table', 'table_copy2', FALSE);
489+
490+
// Rename table
491+
$db->renameTable(array('table_copy' => 'table_copy3'));
492+
493+
// Swap table names
494+
$db->renameTable(array('table_copy3' => 'tmp_table', 'table_copy2' => 'table_copy3', 'tmp_table' => 'table_copy3'));
495+
496+
// Truncate table (empty)
497+
$db->truncateTable('table_copy2');
498+
499+
// Drop one table
500+
$db->dropTable('table_copy4');
501+
502+
// Drop multiple tables
503+
$db->dropTable(array('table_copy3', 'table_copy2'));
504+
505+
// Close connection
506+
$db->close();
507+
```
508+
509+
*Get database size*
510+
```php
511+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
512+
513+
// Connect
514+
$db->connect();
515+
516+
/** Data Base size in B / KB / MB / GB / TB
517+
* @param string $sizeIn - Size in B / KB / MB / GB / TB
518+
* @param integer $round - Round on decimals
519+
* @param resource $link - Link identifier
520+
* @return - Size in B / KB / MB / GB / TB
521+
*/
522+
// function getDataBaseSize($sizeIn = 'MB', $round = 2, $link = 0);
523+
524+
echo 'Database size is: ', $db->getDataBaseSize('mb', 2), ' MB';
525+
526+
// Close connection
527+
$db->close();
528+
```
529+
530+
*Loging queries and errors*
531+
```php
532+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
533+
534+
// Connect to host
535+
$db->connect();
536+
537+
// Default is FALSE, use TRUE only for debuging
538+
$db->logQueries = TRUE;
539+
540+
// This is useful to be TRUE!
541+
$db->logErrors = TRUE;
542+
543+
// Default is FALSE, use TRUE only for debuging (security reasons!)
544+
$db->displayError = TRUE;
545+
546+
// Date / Time format for log
547+
$db->dateFormat = "Y-m-d H:i:s";
548+
549+
// Log file
550+
$db->logFilePath = 'log-mysql.txt';
551+
552+
// Query for this function will be logged
553+
$db->getColumns('table');
554+
555+
// This query has error
556+
$db->query('SELECT * FROM `table` asfd!@#$');
557+
558+
// Close connection
559+
$db->close();
259560
```

0 commit comments

Comments
 (0)