-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.php
732 lines (616 loc) · 27.2 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/*
PHP MySQL Wrapper Exmaples
PHP version required (PHP 5)
*/
require "MySQL_wrapper.class.php";
// set your connectivity settings here
define('MySQL_HOST', 'localhost');
define('MySQL_USER', 'root');
define('MySQL_PASS', '');
define('MySQL_DB', 'test');
/*
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
// Test table sql for examples
$db->query("CREATE TABLE IF NOT EXISTS `table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(250) NOT NULL,
`surname` varchar(250) NOT NULL,
`email` varchar(500) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;");
// Some dummy data, uncomment to insert
if (!$db->countRows('table') > 0)
$db->query("INSERT INTO `table` (`id`, `firstname`, `surname`, `email`, `date`) VALUES
(1, 'Radovan', 'Janjic', '', '2012-11-04'),
(2, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
(3, 'Radovan', 'Janjic''', 'rade@it-radionica.com', '2012-11-04'),
(4, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
(5, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
(6, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
(7, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
(8, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
(9, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04'),
(10, 'Radovan', 'Janjic', 'rade@it-radionica.com', '2012-11-04');");
// Close connection
$db->close();
*/
///////////////////////////////////////////////////////////////////////////////////////////
// Example 1
// Connection example
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
//
// ... do queries
//
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 2
// Connection example
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance();
// connect 1
$db->connect(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB); // You can use connection info here as well
//
// Connection 1 queries
//
// Close connection 1
$db->close();
// Connect 2
$db->connect(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
//
// Connection 2 queries
//
// Close connection 2
$db->close();
// Connect with new link
$db->connect(TRUE);
//
// Connection 3 queries
//
// Close connection 3
$db->close();
// Example 3
// Connection example multi host, db manipulation
///////////////////////////////////////////////////////////////////////////////////////////
// Host 1 instance
$db1 = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Host 2 instance (MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB) -> use another connection info
$db2 = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect host 1
$db1->connect();
// Connect host 2
$db2->connect();
//
// ... do queries of cennection 1 or connection 2
//
// Close connection host 1
$db1->close();
// Close connection host 2
$db2->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 4
// Select example with fetch result
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
// MySQL query
$db->query('SELECT * FROM `table`;');
// Int affected rows
if ($db->affected) {
echo "<hr /><strong>Example 4 ( fetch row - array)</strong><pre>";
while ($row = $db->fetchArray()) {
print_r($row);
}
echo "</pre>";
}
$db->freeResult();
// Escape string
$var = '\'';
$db->query("SELECT * FROM `table` WHERE `firstname` LIKE '{$db->escape($var)}';");
// Param to be escaped
$db->query("SELECT * FROM `table` WHERE `firstname` LIKE '@1%' OR `surname` LIKE '%@1%';", 'rado');
// Params as args
$db->query("SELECT * FROM `table` WHERE `firstname` LIKE '@1%' AND `surname` LIKE '%@2%' OR id = @3;", 'rado', 'janjic', 3 /* , ... */);
// Array of params
$params = array();
$params['id'] = 1;
$params['name'] = 'rado';
$params['lname'] = 'janjic';
$params['limit'] = 5;
$db->query("SELECT * FROM `table` WHERE `firstname` LIKE '@name%' AND `surname` LIKE '%@lname%' OR `id` = @id LIMIT @limit;", $params);
// Int affected rows
if ($db->affected) {
echo "<hr /><strong>Example 4 ( fetch row - array)</strong><pre>";
while ($row = $db->fetchArray()) {
print_r($row);
}
echo "</pre>";
}
$db->freeResult();
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 5
// Prepared statements (works only with MySQLi!)
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
$name = 'Radovan';
$stmt = $db->call('prepare', 'SELECT * FROM `table` WHERE `firstname` = ?;');
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// do something
// print_r($row);
// ...
}
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 5
// Faster select exmaple (fetch query to array)
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
echo "<hr /><strong>Example 5 (fetch query to array)</strong><pre>";
print_r($db->fetchQueryToArray('SELECT * FROM `table`'));
// Returns only first row
print_r($db->fetchQueryToArray('SELECT * FROM `table`', TRUE));
echo "</pre>";
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Exmaple 6
// Multi results
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
// Result 1
$r1 = $db->query('SELECT * FROM `table`');
// Result 2
$r2 = $db->query('SELECT * FROM `table` LIMIT 2');
// Result 1 data
echo "<hr /><strong>Example 6 (multi results)</strong><br> Result 1:<pre>";
if ($db->numRows($r1)) {
while ($row = $db->fetchArray($r1)) {
print_r($row);
}
}
echo "</pre>\nResult 2:\n<pre>";
// Result 2 data
if ($db->numRows($r2)) {
while ($row = $db->fetchArray($r2)) {
print_r($row);
}
}
echo "</pre>";
// Free relust 1
$db->freeResult($r1);
// Free relust 2
$db->freeResult($r2);
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 7
// Rows, Cols num
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
$db->query('SELECT * FROM `table`;');
$cols = $db->numFields();
$rows = $db->numRows();
echo "<hr /><strong>Example 7 (num rows, cols)</strong><br />Cols: {$cols}, Rows: {$rows}<br />";
$db->freeResult();
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 8
// Count rows
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
// Count all
$count = $db->countRows('table');
// Count with condition
$count2 = $db->countRows('table', "`date` = '".date("Y-m-d")."'");
echo "<hr /><strong>Example 8 (count rows)</strong><br />Count all: {$count}, Count today: {$count2}<br />";
// More info
/** Retrieves the number of rows from table based on certain conditions.
* @param string $table - Table name
* @param string $where - WHERE Clause
* @return integer or false
*/
// $db->countRows($table, $where = NULL)
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 9
// Array to insert
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
// Array data
// [fealdname] = feald value
$data = array();
$data['firstname'] = 'Radovan';
$data['surname'] = 'Janjic';
$data['email'] = 'rade@it-radionica.com';
// reserved values 'null', 'now()', 'curtime()', 'localtime()', 'localtime', 'utc_date()', 'utc_time()', 'utc_timestamp()'
$data['date'] = 'now()';
// $db->arrayToInsert( ... ) returns insert id
$insert_id = $db->arrayToInsert('table', $data);
echo "<hr /><strong>Example 9 (array to insert)</strong><br />Last insert id is: {$insert_id}<br />";
// Array data
// [fealdname] = feald value
$data = array();
$data['firstname'] = 'Radovan';
$data['surname'] = 'Janjic';
$data['email'] = 'rade@it-radionica.com';
$data['date'] = 'now()';
// [fealdname] = feald value
$data2 = array();
$data2['firstname'] = 'Radovan';
$data2['surname'] = 'Janjic';
$data2['email'] = 'rade@it-radionica.com';
$data2['date'] = 'now()';
// $db->arrayToInsert( ... ) multirow returns TRUE on success
$db->arrayToInsert('table', array($data, $data2 /*, $data3 .... */ ));
// More options
/** Creates an sql string from an associate array
* @param string $table - Table name
* @param array $data - Data array Eg. $data['column'] = 'val';
* @param boolean $ingore - INSERT IGNORE (row won't actually be inserted if it results in a duplicate key)
* @param string $duplicateupdate - ON DUPLICATE KEY UPDATE (The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.)
* @return insert id or false
*/
// $db->arrayToInsert($table, $data, $ignore = FALSE, $duplicateupdate = NULL)
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 10
// Next AutoIncrement
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
// Returns next auto increment value
$auto_increment = $db->nextAutoIncrement('table');
echo "<hr /><strong>Example 10 (next auto increment)</strong><br>Next auto increment id is: {$auto_increment}<br />";
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 11
// Array to update
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
// Array data
// [fealdname] = feald value
$data = array();
$data['firstname'] = 'Radovan';
$data['surname'] = 'Janjic';
// Reserved values: null, now(), curtime(), localtime(), localtime, utc_date(), utc_time(), utc_timestamp()
$data['email'] = 'null';
$data['date'] = 'now()';
$db->arrayToUpdate('table', $data, "`id` = {$insert_id}");
if ($db->affected) {
echo "<hr /><strong>Example 11 (array to update)</strong><br />Updated: {$db->affected} row(s).<br />";
}
// Array data
// [fealdname] = feald value
$data = array();
$data['id'] = 1; // key
$data['firstname'] = 'foo';
$data['surname'] = 'bar';
$data['email'] = 'rade@it-radionica.com';
$data['date'] = 'now()';
// [fealdname] = feald value
$data2 = array();
$data2['id'] = 2; // key
$data2['firstname'] = 'Radovana';
$data2['surname'] = 'Janjic';
$data2['email'] = 'rade@it-radionica.com';
$data2['date'] = 'now()';
// $db->arrayToUpdate( ... ) multirow returns TRUE on success
$db->arrayToUpdate('table', array($data, $data2 /*, $data3 .... */ ));
// More options
/** Creates an sql string from an associate array
* @param string $table - Table name
* @param array $data - Data array Eg. $data['column'] = 'val';
* @param string $where - MySQL WHERE Clause
* @param integer $limit - Limit offset
* @param resource $link - link identifier
* @return number of updated rows or false
*/
// $db->arrayToUpdate($table, $data, $where = NULL, $limit = 0, $link = 0)
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 12
// Delete row
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
$db->deleteRow('table', "`id` = {$insert_id}");
if ($db->affected) {
echo "<hr><strong>Example 12 (delete row)</strong><br />Deleted: {$db->affected} row(s).<br />";
}
// More options
/** Delete row(s) from table based on certain conditions.
* @param string $table - Table name
* @param string $where - WHERE Clause
* @param integer $limit - Limit offset
* @param resource $link - link identifier
* @return number of deleted rows or false
*/
// $db->deleteRow($table, $where = NULL, $limit = 0, $link = 0)
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 13
// Get table columns
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
echo "<hr /><strong>Example 13 (get table columns)</strong><br />Table columns are:<br />";
print_r($db->getColumns('table'));
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 14
// Basic Table Operation
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
// Copy table (with data included)
$db->copyTable('table', 'table_copy');
// Copy table (with data included)
$db->copyTable('table', 'table_copy4');
// Copy table structure
$db->copyTable('table', 'table_copy2', FALSE);
// Rename table
$db->renameTable(array('table_copy' => 'table_copy3'));
// Swap table names
$db->renameTable(array('table_copy3' => 'tmp_table', 'table_copy2' => 'table_copy3', 'tmp_table' => 'table_copy3'));
// Truncate table (empty)
$db->truncateTable('table_copy2');
// Drop one table
$db->dropTable('table_copy4');
// Drop multiple tables
$db->dropTable(array('table_copy3', 'table_copy2'));
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 15
// Get database size
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
/** Data Base size in B / KB / MB / GB / TB
* @param string $sizeIn - Size in B / KB / MB / GB / TB
* @param integer $round - Round on decimals
* @param resource $link - Link identifier
* @return - Size in B / KB / MB / GB / TB
*/
// function getDataBaseSize($sizeIn = 'MB', $round = 2, $link = 0)
echo '<hr /><pre>Database size is: ', $db->getDataBaseSize('mb', 2), ' MB</pre>';
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 16
// Loging queries and errors
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
$db->logQueries = TRUE; // Default is FALSE, use TRUE only for debuging
$db->logErrors = TRUE; // This is useful to be TRUE!
$db->displayError = TRUE; // Default is FALSE, use TRUE only for debuging (security reasons!)
$db->dateFormat = "Y-m-d H:i:s"; // Date / Time format for log
$db->logFilePath = 'log-mysql.txt'; // Log file
echo "<hr /><strong>Example 14 </strong><br>Loging queries and errors.<br />";
// Query for this function will be logged
$db->getColumns('table');
// This query has error
$db->query('SELECT * FROM `table` asfd!@#$');
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 17
// Export Table to CSV
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
// Export all data
$db->exportTable2CSV('table', 'test_files/test-1.txt');
// Export two or more columns
$db->exportTable2CSV('table', 'test_files/test-2.txt', 'firstname, surname');
// Export two or more columns using array
$db->exportTable2CSV('table', 'test_files/test-3.txt', array('firstname', 'surname', 'date'));
// Export all columns where id < 8 and limit 1, 5
$db->exportTable2CSV('table', 'test_files/test-4.txt', '*', 'id < 8', '1,5');
// More options
/** Export table data to CSV file.
* @param string $table - Table name
* @param string $file - CSV File path
* @param mixed $columns - SQL ( * or column names or array with column names)
* @param string $where - MySQL WHERE Clause
* @param integer $limit - Limit offset
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
* @param string $escape - ESCAPED BY (Default: '\')
* @param string $newLine - New line detelimiter (Default: \n)
* @param boolean $showColumns - Columns names in first line
* @return number of inserted rows or false
*/
// $db->exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit = 0, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE);
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 18
// Query to CSV
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
/** Export query to CSV file.
* @param string $sql - MySQL Query
* @param string $file - CSV File path
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
* @param string $escape - ESCAPED BY (Default: '\')
* @param string $newLine - New line delimiter (Default: \n)
* @param boolean $showColumns - Columns names in first line
* @return - File path
*/
// function query2CSV($sql, $file, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE)
$path = $db->query2CSV('select * from `table` limit 10', 'test_files/test-query2csv.csv');
echo '<hr /><pre>Query exported to CSV file: ', $path, '</pre>';
// example 2
$path = $db->query2CSV('select * from `table` limit 2,2', 'test_files/test-query2csv.csv');
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 19
// Import CSV to Table
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
// Import all data
$db->importCSV2Table('test_files/test-1.txt', 'table');
// More options
/** Imports CSV data to Table with possibility to update rows while import.
* @param string $file - CSV File path
* @param string $table - Table name
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
* @param string $escape - ESCAPED BY (Defaul: '\')
* @param integer $ignore - Number of ignored rows (Default: 1)
* @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1')
* @param string $getColumnsFrom - Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)
* @param string $newLine - New line detelimiter (Default: \n)
* @return number of inserted rows or false
*/
// $db->importCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n')
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 20
// Create table from CSV file
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
$db->connect();
$db->dropTable('csv_to_table_test');
$db->createTableFromCSV('test_files/countrylist.csv', 'csv_to_table_test');
$db->dropTable('csv_to_table_test_no_column_names');
$db->createTableFromCSV('test_files/countrylist1.csv', 'csv_to_table_test_no_column_names', ',', '"', '\\', 0, array(), 'generate', '\r\n');
/** Create table from CSV file and imports CSV data to Table with possibility to update rows while import.
* @param string $file - CSV File path
* @param string $table - Table name
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
* @param string $escape - ESCAPED BY (Default: '\')
* @param integer $ignore - Number of ignored rows (Default: 1)
* @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1')
* @param string $getColumnsFrom - Get Columns Names from (file or generate) - this is important if there is update while inserting (Default: file)
* @param string $newLine - New line delimiter (Default: \n)
* @return number of inserted rows or false
*/
// function createTableFromCSV($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\r\n')
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 21
// Import CSV to Table
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
// Import and update all data
$db->importUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test');
// Import and update all data
$db->importUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test', ',', '"', '\\', 1, array(), 'file', '\r\n');
// More options
/** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import.
* @param string $file - CSV File path
* @param string $table - Table name
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
* @param string $escape - ESCAPED BY (Defaul: '\')
* @param integer $ignore - Number of ignored rows (Default: 1)
* @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1')
* @param string $getColumnsFrom - Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)
* @param string $newLine - New line detelimiter (Default: \n)
* @return number of inserted rows or false
*/
// $db->importUpdateCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n')
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 22
// Transactions
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
$queries = array();
$queries[] = 'SELECT ...';
$queries[] = 'INSERT ...';
$queries[] = 'DELETE ...';
$queries[] = '...';
//$db->transaction($queries);
// Get more info on: http://dev.mysql.com/doc/refman/5.0/en/commit.html
/** Transaction
* @param array $qarr - Array with Queries
* @link http://dev.mysql.com/doc/refman/5.0/en/commit.html
*/
// function transaction($qarr = array())
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 23
// String Search and Replace in all or defined Table Columns
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
// Simple
$db->strReplace('table', 'firstname', 'search', 'replace');
// Search array & Replace string
$db->strReplace('table', 'firstname', array('search1', 'search2'), 'replace');
// Search array & Replace array
$db->strReplace('table', 'firstname', array('search1', 'search2'), array('replace1', 'replace2'));
// Search array of columns (Search array & Replace array) return count of updated fielsd
$count = $db->strReplace('table', array('firstname', 'surname'), array('search1', 'search2'), array('replace1', 'replace2'));
// String multiple columns
$db->strReplace('table', 'firstname, surname', 'search', 'replace');
// You can set all columns in table as well
$db->strReplace('table', '*', 'search', 'replace');
// More options
/** Replace all occurrences of the search string with the replacement string in MySQL Table Column(s).
* @param string $table - Table name
* @param mixed $columns - Search & Replace affected Table columns. An array may be used to designate multiple replacements.
* @param mixed $search - The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
* @param mixed $replace - The replacement value that replaces found search values. An array may be used to designate multiple replacements.
* @param string $where - WHERE Clause
* @param integer $limit - Limit offset
* @return integer - Affected rows
*/
// function strReplace($table, $columns, $search, $replace, $where = NULL, $limit = 0)
// Close connection
$db->close();
///////////////////////////////////////////////////////////////////////////////////////////
// Example 24
// E-mail on error / die on error
///////////////////////////////////////////////////////////////////////////////////////////
$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
// Connect
$db->connect();
$db->emailErrors = TRUE;
$db->dieOnError = TRUE;
$db->emailErrorsTo = array('rade@it-radionica.com');
$db->query("select * from asdf");
$db->query("select * from asdf2"); // this one will not be executed because dieOnError = TRUE
// Close connection
$db->close();