@@ -2,25 +2,33 @@ PHP_MySQL_wrapper
22=================
33
44This class implements a generic MySQL database access wrapper.
5- It can:
5+
66* [ Connect to a given MySQL server] ( #connect-to-a-given-mysql-server )
77 * [ Connection examples] ( #connection-examples )
88 * [ Connection example multi host, db manipulation] ( #connection-example-multi-host-db-manipulation )
99* [ Set the connection character set encoding] ( #set-the-connection-character-set-encoding )
10- * Execute arbitrary queries and return the results in arrays
10+ * [ Execute arbitrary queries and return the results in arrays] ( #execute-arbitrary-queries-and-return-the-results-in-arrays )
11+ * [ Select example with fetch result] ( #select-example-with-fetch-result )
12+ * [ Prepared statements] ( #prepared-statements-works-only-with-mysqli )
13+ * [ Prepared statements - mysqlnd driver not installed] ( #prepared-statements-works-only-with-mysqli---if-mysqlnd-driver-is-not-installed )
14+ * [ Fetch query to array] ( #fetch-query-to-array )
15+ * [ Multi results] ( #multi-results )
16+ * [ Rows, Cols num] ( #rows-cols-num )
17+ * [ Count rows] ( #count-rows )
1118* Execute UPDATE or INSERT queries from parameters that define the tables, fields, field values and conditions
12- * Array to insert
13- * Array to update
14- * Multiple INSERT / UPDATE
19+ * [ Array to insert] ( #array-to-insert )
20+ * [ Multiple array to insert] ( #array-to-insert )
21+ * [ Array to update] ( #array-to-update )
22+ * [ Multiple array to update] ( #array-to-update )
1523* [ Count the number of rows of a table that match a given condition] ( #count-rows )
1624* [ Delete table rows that match a given condition] ( #delete-rows )
17- * Operations with CSV files
25+ * [ Operations with CSV files] ( #operations-with-csv-files )
1826 * [ Export table to CSV] ( #export-table-to-csv )
1927 * [ Export query to CSV] ( #export-query-to-csv )
2028 * [ Import CSV to Table] ( #import-csv-to-table )
2129 * [ Import and update CSV to Table] ( #import-and-update-csv-to-table )
2230 * [ Create table from CSV file] ( #create-table-from-csv-file )
23- * Do str_replace in given database, table or defined columns in table
31+ * [ Do str_replace in given database, table or defined columns in table] ( #string-search-and-replace-in-all-or-defined-table-columns )
2432* [ Basic table operations] ( #basic-table-operation )
2533 * [ Copy table (with data included)] ( #basic-table-operation )
2634 * [ Copy table structure] ( #basic-table-operation )
@@ -32,6 +40,7 @@ It can:
3240 * [ Get table columns] ( #get-table-columns )
3341 * [ Get database size] ( #get-database-size )
3442 * [ Get the next value of an auto-incremented table field] ( #next-autoincrement )
43+ * [ Table revision] ( #table-revision )
3544* [ Log queries / errors] ( #loging-queries-and-errors )
3645* Errors backtrace
3746
@@ -149,7 +158,9 @@ $db->setCharset('utf8');
149158$db->close();
150159```
151160
152- ### Select example with fetch result
161+ ### Execute arbitrary queries and return the results in arrays
162+
163+ #### Select example with fetch result
153164``` php
154165$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
155166
@@ -207,7 +218,7 @@ $db->freeResult();
207218$db->close();
208219```
209220
210- ### Prepared statements (works only with MySQLi!)
221+ #### Prepared statements (works only with MySQLi!)
211222``` php
212223$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
213224
@@ -235,7 +246,7 @@ while ($row = $result->fetch_assoc()) {
235246$db->close();
236247```
237248
238- ### Prepared statements (works only with MySQLi!) - if mysqlnd driver is not installed
249+ #### Prepared statements (works only with MySQLi!) - if mysqlnd driver is not installed
239250``` php
240251$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
241252
@@ -264,7 +275,7 @@ print_r($data);
264275$db->close();
265276```
266277
267- ### Fetch query to array
278+ #### Fetch query to array
268279``` php
269280$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
270281
@@ -287,7 +298,7 @@ print_r($array);
287298$db->close();
288299```
289300
290- ### Multi results
301+ #### Multi results
291302``` php
292303$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
293304
@@ -326,7 +337,7 @@ $db->freeResult($r2);
326337$db->close();
327338```
328339
329- ### Rows, Cols num
340+ #### Rows, Cols num
330341``` php
331342$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
332343
@@ -349,7 +360,7 @@ $db->freeResult();
349360$db->close();
350361```
351362
352- ### Count rows
363+ #### Count rows
353364``` php
354365$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
355366
@@ -376,8 +387,9 @@ echo "Count all: {$count}, Count today: {$count2}";
376387// Close connection
377388$db->close();
378389```
390+ ### Execute UPDATE or INSERT queries from parameters that define the tables, fields, field values and conditions
379391
380- ### Array to insert
392+ #### Array to insert
381393``` php
382394$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
383395
@@ -429,23 +441,8 @@ $db->arrayToInsert('table', array($data, $data2 /*, $data3 .... */ ));
429441// Close connection
430442$db->close();
431443```
432- ### Next AutoIncrement
433- ``` php
434- $db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
435-
436- // Connect to host
437- $db->connect();
438-
439- // Returns next auto increment value
440- $auto_increment = $db->nextAutoIncrement('table');
441444
442- echo "Next auto increment id is: {$auto_increment}";
443-
444- // Close connection
445- $db->close();
446- ```
447-
448- ### Array to update
445+ #### Array to update
449446``` php
450447$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
451448
@@ -581,7 +578,7 @@ $db->dropTable(array('table_copy3', 'table_copy2'));
581578$db->close();
582579```
583580
584- ### Get database size
581+ #### Get database size
585582``` php
586583$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
587584
@@ -598,6 +595,58 @@ $db->connect();
598595
599596echo 'Database size is: ', $db->getDataBaseSize('mb', 2), ' MB';
600597
598+ // Close connection
599+ $db->close();
600+ ```
601+ #### Next AutoIncrement
602+ ``` php
603+ $db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
604+
605+ // Connect to host
606+ $db->connect();
607+
608+ // Returns next auto increment value
609+ $auto_increment = $db->nextAutoIncrement('table');
610+
611+ echo "Next auto increment id is: {$auto_increment}";
612+
613+ // Close connection
614+ $db->close();
615+ ```
616+
617+ #### Table revision
618+ ``` php
619+
620+ $db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
621+
622+ // Connect
623+ $db->connect();
624+
625+ // Init table revision (do this only once!)
626+ $db->initTableRevision('rev-table');
627+
628+ // Time to restore to ...
629+ $time = '2014-06-25 14:26:03';
630+
631+ /** Create table from current revision time
632+ * @param string $table - New table name
633+ * @param string $rev_table - Revision table (origin table)
634+ * @param string $id_field - Unique field name
635+ * @param datetime - Revision time
636+ */
637+ // $db->createTableFromRevisionTime($table, $rev_table, $id_field, $time);
638+
639+ $db->createTableFromRevisionTime('rev-table' . '-' . $time, 'rev-table', 'id', $time);
640+
641+ /** Restore table from current revision time
642+ * @param string $table - New table name
643+ * @param string $id_field - Unique field name
644+ * @param datetime - Revision time
645+ */
646+ //$db->restoreTableFromRevisionTime($table, $id_field, $time);
647+
648+ $db->restoreTableFromRevisionTime('rev-table', 'id', $time);
649+
601650// Close connection
602651$db->close();
603652```
@@ -634,7 +683,9 @@ $db->query('SELECT * FROM `table` asfd!@#$');
634683$db->close();
635684```
636685
637- ### Export Table to CSV
686+ ### Operations with CSV files
687+
688+ #### Export Table to CSV
638689``` php
639690$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
640691
@@ -673,7 +724,7 @@ $db->exportTable2CSV('table', 'test_files/test-4.txt', '*', 'id < 8', '1,5');
673724$db->close();
674725```
675726
676- ### Export query to CSV
727+ #### Export query to CSV
677728``` php
678729$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
679730
@@ -702,7 +753,7 @@ $path = $db->query2CSV('select * from `table` limit 2,2', 'test_files/test-query
702753$db->close();
703754```
704755
705- ### Import CSV to Table
756+ #### Import CSV to Table
706757``` php
707758$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
708759
@@ -731,63 +782,63 @@ $db->importCSV2Table('test_files/test-1.txt', 'table');
731782$db->close();
732783```
733784
734- ### Create table from CSV file
785+ #### Import and update CSV to Table
735786``` php
736787$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
737788
738- // Connect to host
739- $db->connect();
740-
741- $db->dropTable('csv_to_table_test');
742- $db->createTableFromCSV('test_files/countrylist.csv', 'csv_to_table_test');
789+ // Connect
790+ $db->connect();
743791
744- $db->dropTable('csv_to_table_test_no_column_names');
745- $db->createTableFromCSV ('test_files/countrylist1 .csv', 'csv_to_table_test_no_column_names', ',', '"', '\\', 0, array(), 'generate', '\r\n ');
792+ // Import and update all data
793+ $db->importUpdateCSV2Table ('test_files/countrylist .csv', 'csv_to_table_test ');
746794
747- /** Create table from CSV file and imports CSV data to Table with possibility to update rows while import.
795+ // Import and update all data
796+ $db->importUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test', ',', '"', '\\', 1, array(), 'file', '\r\n');
797+ // More options
798+ /** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import.
748799 * @param string $file - CSV File path
749800 * @param string $table - Table name
750801 * @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
751802 * @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
752- * @param string $escape - ESCAPED BY (Default : '\')
803+ * @param string $escape - ESCAPED BY (Defaul : '\')
753804 * @param integer $ignore - Number of ignored rows (Default: 1)
754805 * @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')
755- * @param string $getColumnsFrom - Get Columns Names from (file or generate ) - this is important if there is update while inserting (Default: file)
756- * @param string $newLine - New line delimiter (Default: \n)
806+ * @param string $getColumnsFrom - Get Columns Names from (file or table ) - this is important if there is update while inserting (Default: file)
807+ * @param string $newLine - New line detelimiter (Default: \n)
757808 * @return number of inserted rows or false
758809 */
759- // function createTableFromCSV ($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\r \n');
810+ // $db->importUpdateCSV2Table ($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n');
760811
761812// Close connection
762813$db->close();
763814```
764815
765- ### Import and update CSV to Table
816+ #### Create table from CSV file
766817``` php
767818$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
768819
769- // Connect
770- $db->connect();
820+ // Connect to host
821+ $db->connect();
771822
772- // Import and update all data
773- $db->importUpdateCSV2Table ('test_files/countrylist.csv', 'csv_to_table_test');
823+ $db->dropTable('csv_to_table_test');
824+ $db->createTableFromCSV ('test_files/countrylist.csv', 'csv_to_table_test');
774825
775- // Import and update all data
776- $db->importUpdateCSV2Table ('test_files/countrylist .csv', 'csv_to_table_test ', ',', '"', '\\', 1 , array(), 'file ', '\r\n');
777- // More options
778- /** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import.
826+ $db->dropTable('csv_to_table_test_no_column_names');
827+ $db->createTableFromCSV ('test_files/countrylist1 .csv', 'csv_to_table_test_no_column_names ', ',', '"', '\\', 0 , array(), 'generate ', '\r\n');
828+
829+ /** Create table from CSV file and imports CSV data to Table with possibility to update rows while import.
779830 * @param string $file - CSV File path
780831 * @param string $table - Table name
781832 * @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
782833 * @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
783- * @param string $escape - ESCAPED BY (Defaul : '\')
834+ * @param string $escape - ESCAPED BY (Default : '\')
784835 * @param integer $ignore - Number of ignored rows (Default: 1)
785836 * @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')
786- * @param string $getColumnsFrom - Get Columns Names from (file or table ) - this is important if there is update while inserting (Default: file)
787- * @param string $newLine - New line detelimiter (Default: \n)
837+ * @param string $getColumnsFrom - Get Columns Names from (file or generate ) - this is important if there is update while inserting (Default: file)
838+ * @param string $newLine - New line delimiter (Default: \n)
788839 * @return number of inserted rows or false
789840 */
790- // $db->importUpdateCSV2Table ($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n');
841+ // function createTableFromCSV ($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\r \n');
791842
792843// Close connection
793844$db->close();
@@ -888,39 +939,4 @@ $db->query("select * from asdf2");
888939$db->close();
889940```
890941
891- ### Table revision
892- ``` php
893-
894- $db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);
895942
896- // Connect
897- $db->connect();
898-
899- // Init table revision (do this only once!)
900- $db->initTableRevision('rev-table');
901-
902- // Time to restore to ...
903- $time = '2014-06-25 14:26:03';
904-
905- /** Create table from current revision time
906- * @param string $table - New table name
907- * @param string $rev_table - Revision table (origin table)
908- * @param string $id_field - Unique field name
909- * @param datetime - Revision time
910- */
911- // $db->createTableFromRevisionTime($table, $rev_table, $id_field, $time);
912-
913- $db->createTableFromRevisionTime('rev-table' . '-' . $time, 'rev-table', 'id', $time);
914-
915- /** Restore table from current revision time
916- * @param string $table - New table name
917- * @param string $id_field - Unique field name
918- * @param datetime - Revision time
919- */
920- //$db->restoreTableFromRevisionTime($table, $id_field, $time);
921-
922- $db->restoreTableFromRevisionTime('rev-table', 'id', $time);
923-
924- // Close connection
925- $db->close();
926- ```
0 commit comments