Skip to content

Commit 59107d7

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

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed

README.md

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,257 @@ $db->query('SELECT * FROM `table` asfd!@#$');
558558
// Close connection
559559
$db->close();
560560
```
561+
562+
*Export Table to CSV*
563+
```php
564+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
565+
566+
// Connect
567+
$db->connect();
568+
569+
// Export all data
570+
$db->exportTable2CSV('table', 'test_files/test-1.txt');
571+
572+
// Export two or more columns
573+
$db->exportTable2CSV('table', 'test_files/test-2.txt', 'firstname, surname');
574+
575+
// Export two or more columns using array
576+
$db->exportTable2CSV('table', 'test_files/test-3.txt', array('firstname', 'surname', 'date'));
577+
578+
// Export all columns where id < 8 and limit 1, 5
579+
$db->exportTable2CSV('table', 'test_files/test-4.txt', '*', 'id < 8', '1,5');
580+
581+
// More options
582+
/** Export table data to CSV file.
583+
* @param string $table - Table name
584+
* @param string $file - CSV File path
585+
* @param mixed $columns - SQL ( * or column names or array with column names)
586+
* @param string $where - MySQL WHERE Clause
587+
* @param integer $limit - Limit offset
588+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
589+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
590+
* @param string $escape - ESCAPED BY (Default: '\')
591+
* @param string $newLine - New line detelimiter (Default: \n)
592+
* @param boolean $showColumns - Columns names in first line
593+
* @return number of inserted rows or false
594+
*/
595+
// $db->exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit = 0, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE);
596+
597+
// Close connection
598+
$db->close();
599+
```
600+
601+
*Query to CSV*
602+
```php
603+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
604+
605+
// Connect
606+
$db->connect();
607+
608+
$path = $db->query2CSV('select * from `table` limit 10', 'test_files/test-query2csv.csv');
609+
echo 'Query exported to CSV file: ', $path;
610+
611+
// Example 2
612+
$path = $db->query2CSV('select * from `table` limit 2,2', 'test_files/test-query2csv.csv');
613+
614+
/** Export query to CSV file.
615+
* @param string $sql - MySQL Query
616+
* @param string $file - CSV File path
617+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
618+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
619+
* @param string $escape - ESCAPED BY (Default: '\')
620+
* @param string $newLine - New line delimiter (Default: \n)
621+
* @param boolean $showColumns - Columns names in first line
622+
* @return - File path
623+
*/
624+
// function query2CSV($sql, $file, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE);
625+
626+
// Close connection
627+
$db->close();
628+
```
629+
630+
*Import CSV to Table*
631+
```php
632+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
633+
634+
// Connect
635+
$db->connect();
636+
637+
// Import all data
638+
$db->importCSV2Table('test_files/test-1.txt', 'table');
639+
640+
// More options
641+
/** Imports CSV data to Table with possibility to update rows while import.
642+
* @param string $file - CSV File path
643+
* @param string $table - Table name
644+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
645+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
646+
* @param string $escape - ESCAPED BY (Defaul: '\')
647+
* @param integer $ignore - Number of ignored rows (Default: 1)
648+
* @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')
649+
* @param string $getColumnsFrom - Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)
650+
* @param string $newLine - New line detelimiter (Default: \n)
651+
* @return number of inserted rows or false
652+
*/
653+
// $db->importCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n');
654+
655+
// Close connection
656+
$db->close();
657+
```
658+
659+
*Create table from CSV file*
660+
```php
661+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
662+
663+
// Connect to host
664+
$db->connect();
665+
666+
$db->dropTable('csv_to_table_test');
667+
$db->createTableFromCSV('test_files/countrylist.csv', 'csv_to_table_test');
668+
669+
$db->dropTable('csv_to_table_test_no_column_names');
670+
$db->createTableFromCSV('test_files/countrylist1.csv', 'csv_to_table_test_no_column_names', ',', '"', '\\', 0, array(), 'generate', '\r\n');
671+
672+
/** Create table from CSV file and imports CSV data to Table with possibility to update rows while import.
673+
* @param string $file - CSV File path
674+
* @param string $table - Table name
675+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
676+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
677+
* @param string $escape - ESCAPED BY (Default: '\')
678+
* @param integer $ignore - Number of ignored rows (Default: 1)
679+
* @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')
680+
* @param string $getColumnsFrom - Get Columns Names from (file or generate) - this is important if there is update while inserting (Default: file)
681+
* @param string $newLine - New line delimiter (Default: \n)
682+
* @return number of inserted rows or false
683+
*/
684+
// function createTableFromCSV($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\r\n');
685+
686+
// Close connection
687+
$db->close();
688+
```
689+
690+
*Import and update CSV to Table*
691+
```php
692+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
693+
694+
// Connect
695+
$db->connect();
696+
697+
// Import and update all data
698+
$db->importUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test');
699+
700+
// Import and update all data
701+
$db->importUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test', ',', '"', '\\', 1, array(), 'file', '\r\n');
702+
// More options
703+
/** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import.
704+
* @param string $file - CSV File path
705+
* @param string $table - Table name
706+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
707+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
708+
* @param string $escape - ESCAPED BY (Defaul: '\')
709+
* @param integer $ignore - Number of ignored rows (Default: 1)
710+
* @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')
711+
* @param string $getColumnsFrom - Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)
712+
* @param string $newLine - New line detelimiter (Default: \n)
713+
* @return number of inserted rows or false
714+
*/
715+
// $db->importUpdateCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n');
716+
717+
// Close connection
718+
$db->close();
719+
```
720+
721+
*Transactions*
722+
```php
723+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
724+
725+
// Connect
726+
$db->connect();
727+
728+
// Queries
729+
$queries = array();
730+
$queries[] = 'SELECT ...';
731+
$queries[] = 'INSERT ...';
732+
$queries[] = 'DELETE ...';
733+
$queries[] = '...';
734+
735+
// Do Transaction
736+
$db->transaction($queries);
737+
738+
// Get more info on: http://dev.mysql.com/doc/refman/5.0/en/commit.html
739+
/** Transaction
740+
* @param array $qarr - Array with Queries
741+
* @link http://dev.mysql.com/doc/refman/5.0/en/commit.html
742+
*/
743+
// $db->transaction($qarr = array());
744+
745+
// Close connection
746+
$db->close();
747+
```
748+
749+
*String Search and Replace in all or defined Table Columns*
750+
```php
751+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
752+
753+
// Connect
754+
$db->connect();
755+
756+
// Simple
757+
$db->strReplace('table', 'firstname', 'search', 'replace');
758+
759+
// Search array & Replace string
760+
$db->strReplace('table', 'firstname', array('search1', 'search2'), 'replace');
761+
762+
// Search array & Replace array
763+
$db->strReplace('table', 'firstname', array('search1', 'search2'), array('replace1', 'replace2'));
764+
765+
// Search array of columns (Search array & Replace array) return count of updated fielsd
766+
$count = $db->strReplace('table', array('firstname', 'surname'), array('search1', 'search2'), array('replace1', 'replace2'));
767+
768+
// String multiple columns
769+
$db->strReplace('table', 'firstname, surname', 'search', 'replace');
770+
771+
// You can set all columns in table as well
772+
$db->strReplace('table', '*', 'search', 'replace');
773+
774+
// More options
775+
/** Replace all occurrences of the search string with the replacement string in MySQL Table Column(s).
776+
* @param string $table - Table name
777+
* @param mixed $columns - Search & Replace affected Table columns. An array may be used to designate multiple replacements.
778+
* @param mixed $search - The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
779+
* @param mixed $replace - The replacement value that replaces found search values. An array may be used to designate multiple replacements.
780+
* @param string $where - WHERE Clause
781+
* @param integer $limit - Limit offset
782+
* @return integer - Affected rows
783+
*/
784+
// function strReplace($table, $columns, $search, $replace, $where = NULL, $limit = 0);
785+
786+
// Close connection
787+
$db->close();
788+
```
789+
790+
*E-mail on error / die on error*
791+
```php
792+
$db = new MySQL_wrapper(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
793+
794+
// Connect
795+
$db->connect();
796+
797+
// Send mail on error
798+
$db->emailErrors = TRUE;
799+
800+
// Die on errors
801+
$db->dieOnError = TRUE;
802+
803+
// Array of emails
804+
$db->emailErrorsTo = array('rade@it-radionica.com');
805+
806+
// Do first query
807+
$db->query("select * from asdf");
808+
809+
// This one will not be executed if first query have error and dieOnError is TRUE
810+
$db->query("select * from asdf2");
811+
812+
// Close connection
813+
$db->close();
814+
```

0 commit comments

Comments
 (0)