{"id":19600831,"url":"https://github.com/rjanjic/php_mysql_wrapper","last_synced_at":"2025-04-27T16:32:28.782Z","repository":{"id":10848842,"uuid":"13130494","full_name":"rjanjic/PHP_MySQL_wrapper","owner":"rjanjic","description":"MySQL database access wrapper.","archived":false,"fork":false,"pushed_at":"2014-10-29T16:51:14.000Z","size":1069,"stargazers_count":12,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T01:32:22.315Z","etag":null,"topics":["count-rows","csv","csv-files","database","insert-query","mysql","mysql-backup","mysql-database","mysql-wrapper","mysqli","php","prepared-statements","xml-files"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rjanjic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-09-26T18:19:12.000Z","updated_at":"2022-09-23T16:10:30.000Z","dependencies_parsed_at":"2022-09-02T21:02:56.548Z","dependency_job_id":null,"html_url":"https://github.com/rjanjic/PHP_MySQL_wrapper","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rjanjic%2FPHP_MySQL_wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rjanjic%2FPHP_MySQL_wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rjanjic%2FPHP_MySQL_wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rjanjic%2FPHP_MySQL_wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rjanjic","download_url":"https://codeload.github.com/rjanjic/PHP_MySQL_wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251171703,"owners_count":21547146,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["count-rows","csv","csv-files","database","insert-query","mysql","mysql-backup","mysql-database","mysql-wrapper","mysqli","php","prepared-statements","xml-files"],"created_at":"2024-11-11T09:16:17.118Z","updated_at":"2025-04-27T16:32:27.412Z","avatar_url":"https://github.com/rjanjic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"PHP MySQL Wrapper Class\n=======================\n\nThis class implements a generic MySQL database access wrapper. \n\n* [Connect to a given MySQL server](#connect-to-a-given-mysql-server)\n * [Connection examples](#connection-examples)\n * [Connection example multi host, db manipulation](#connection-example-multi-host-db-manipulation)\n* [Set the connection character set encoding](#set-the-connection-character-set-encoding)\n* [Execute arbitrary queries and return the results in arrays](#execute-arbitrary-queries-and-return-the-results-in-arrays)\n * [Select example with fetch result](#select-example-with-fetch-result)\n * [Prepared statements](#prepared-statements-works-only-with-mysqli)\n * [Prepared statements - mysqlnd driver not installed](#prepared-statements-works-only-with-mysqli---if-mysqlnd-driver-is-not-installed)\n * [Fetch query to array](#fetch-query-to-array)\n * [Multi results](#multi-results)\n * [Rows, Cols num](#rows-cols-num)\n * [Count rows](#count-rows)\n* [Execute UPDATE or INSERT queries from parameters that define the tables, fields, field values and conditions](#execute-update-or-insert-queries-from-parameters-that-define-the-tables-fields-field-values-and-conditions)\n * [Array to insert](#array-to-insert)\n * [Multiple array to insert](#array-to-insert-multirow)\n * [Array to update](#array-to-update)\n * [Multiple array to update](#array-to-update-multirow)\n* [Count the number of rows of a table that match a given condition](#count-rows)\n* [Delete table rows that match a given condition](#delete-rows)\n* [Operations with CSV files](#operations-with-csv-files)\n * [Export table to CSV](#export-table-to-csv)\n * [Export query to CSV](#export-query-to-csv)\n * [Export table / export query to CSV using fputcsv](#export-table--export-query-to-csv-using-fputcsv)\n * [Download CSV file from query](#download-csv-file-from-query)\n * [Import CSV to Table](#import-csv-to-table)\n * [Import and update CSV to Table](#import-and-update-csv-to-table)\n * [Create table from CSV file](#create-table-from-csv-file)\n* [Operations with XML files](#operations-with-xml-files)\n * [Export query to XML](#export-query-to-xml)\n * [Download XML file from query](#download-xml-file-from-query)\n* [Do str_replace in given database, table or defined columns in table](#string-search-and-replace-in-all-or-defined-table-columns)\n * [Search string \u0026 replace string](#string-search-and-replace-in-all-or-defined-table-columns)\n * [Search array \u0026 replace string](#string-search-and-replace-in-all-or-defined-table-columns)\n * [Search array \u0026 replace array](#string-search-and-replace-in-all-or-defined-table-columns)\n * [Search array of columns (search array \u0026 replace array) return count of updated](#string-search-and-replace-in-all-or-defined-table-columns)\n * [String multiple columns](#string-search-and-replace-in-all-or-defined-table-columns)\n * [All columns in table](#string-search-and-replace-in-all-or-defined-table-columns)\n * [Whole database](#string-search-and-replace-in-all-or-defined-table-columns)\n* [Basic table operations](#basic-table-operation)\n * [Copy table (with data included)](#basic-table-operation)\n * [Copy table structure](#basic-table-operation)\n * [Rename table](#basic-table-operation)\n * [Swap table names](#basic-table-operation)\n * [Truncate table (empty)](#basic-table-operation)\n * [Drop one table](#basic-table-operation)\n * [Drop multiple tables](#basic-table-operation)\n * [Get table columns](#get-table-columns)\n * [Get database size](#get-database-size)\n * [Get the next value of an auto-incremented table field](#next-autoincrement)\n * [Table revision](#table-revision)\n* [Logging / debug](#loging--debug)\n * [Logging errors](#logging-errors)\n * [Logging queries](#logging-queries)\n * [E-mail on error / die on error](#e-mail-on-error--die-on-error)\n * [Errors backtrace and debug](#errors-backtrace-and-debug)\n\n### Connectivity settings\n```php\n// Set your connectivity settings\ndefine('MySQL_HOST', 'localhost'); // localhost:3306\ndefine('MySQL_USER', 'root');\ndefine('MySQL_PASS', '');\ndefine('MySQL_DB', 'test');\n```\n\n### Connect to a given MySQL server\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect(); \n\n//\n// ... do queries\n//\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Connection examples\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect 1\n$db-\u003econnect();\n\n//\n// Connection 1 queries ...\n//\n\n// Close connection 1\n$db-\u003eclose();\n\n// Connect 2\n$db-\u003econnect(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB); \n\n//\n// Connection 2 queries ...\n//\n\n// Close connection 2\n$db-\u003eclose();\n\n// Connection 3\n$db-\u003econnect();\n\n//\n// Connection 3 queries\n//\n\n// Close connection 3\n$db-\u003eclose();\n```\n\n#### Connection example multi host, db manipulation\n```php\n// Inst. 1\n$db1 = MySQL_wrapper::getInstance('host1', MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Inst. 2\n$db2 = MySQL_wrapper::getInstance('host2', MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect host 1\n$db1-\u003econnect();\n\n// Connect host 2\n$db2-\u003econnect();\n\n//\n// ... do queries of cennection 1 or connection 2\n//\n\n// Close connection host 1\n$db1-\u003eclose();\n\n// Close connection host 2\n$db2-\u003eclose();\n```\n\n### Set the connection character set encoding\n\n#### Example 1\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect(); \n\n// Set charset\n$db-\u003echarset = 'utf8';;\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Example 2\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect(); \n\n// Set charset\n$db-\u003esetCharset('utf8');\n\n// Close connection\n$db-\u003eclose();\n```\n\n### Execute arbitrary queries and return the results in arrays\n\n#### Select example with fetch result\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// MySQL query\n$db-\u003equery('SELECT * FROM `table`');\n\n// Int affected rows\nif ($db-\u003eaffected \u003e 0) {\n\twhile ($row = $db-\u003efetchArray()) {\n\t\t// Result\n\t\tprint_r($row);\n\t}\n}\n\n// Free result memory\n$db-\u003efreeResult();\n\n// Escape string\n$var = '\\'';\n\n// Do query\n$db-\u003equery(\"SELECT * FROM `table` WHERE `firstname` LIKE '{$db-\u003eescape($var)}';\");\n\n// Param to be escaped\n$db-\u003equery(\"SELECT * FROM `table` WHERE `firstname` LIKE '@1%' OR `surname` LIKE '%@1%';\", 'rado');\n\n// Params as args\n$db-\u003equery(\"SELECT * FROM `table` WHERE `firstname` LIKE '@1%' AND `surname` LIKE '%@2%' OR id = @3;\", 'rado', 'janjic', 3 /* , ... */);\n\n// Array of params\n$params = array();\n$params['id'] = 1;\n$params['name'] = 'rado';\n$params['lname'] = 'janjic';\n$params['limit'] = 5;\n\n// Exec query\n$db-\u003equery(\"SELECT * FROM `table` WHERE `firstname` LIKE '@name%' AND `surname` LIKE '%@lname%' OR `id` = @id LIMIT @limit;\", $params);\n\n// Int affected rows\nif ($db-\u003eaffected \u003e 0) {\n\twhile ($row = $db-\u003efetchArray()) {\n\t\t// Print result row\n\t\tprint_r($row);\n\t}\n}\n\n// Free result memory\n$db-\u003efreeResult();\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Prepared statements (works only with MySQLi!)\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Works only with MySQLi!\n$db-\u003eextension = 'mysqli';\n\n// Connect\n$db-\u003econnect();\n\n$name = 'Radovan';\n\n$stmt = $db-\u003ecall('prepare', 'SELECT * FROM `table` WHERE `firstname` = ?;');\n$stmt-\u003ebind_param('s', $name);\n\n$stmt-\u003eexecute();\n\n$result = $stmt-\u003eget_result();\nwhile ($row = $result-\u003efetch_assoc()) {\n    // do something\n\t// print_r($row);\n\t// ...\n}\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Prepared statements (works only with MySQLi!) - if mysqlnd driver is not installed\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n$stmt = $db-\u003ecall('prepare', 'SELECT `id`, `firstname`, `surname`, `email`  FROM `table` WHERE `level` = ?;');\n$stmt-\u003ebind_param('i', $level);\n$stmt-\u003eexecute();\n\n$stmt-\u003ebind_result($id, $firstname, $surname, $email);\n$data = array();\nwhile ($stmt-\u003efetch()) {\n\t$data[] = array(\n\t\t'id' \t\t=\u003e $id,\n\t\t'firstname' =\u003e $firstname,\n\t\t'surname' \t=\u003e $surname,\n\t\t'email' \t=\u003e $email\n\t);\n}\n\n// Print data\nprint_r($data);\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Fetch query to array\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Fetch query to array\n$array = $db-\u003efetchQueryToArray('SELECT * FROM `table`');\n\n// Print array\nprint_r($array);\n\n// Returns only first row\n$array = $db-\u003efetchQueryToArray('SELECT * FROM `table`', TRUE);\n\n// Print array\nprint_r($array);\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Multi results\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Result 1\n$r1 = $db-\u003equery('SELECT * FROM `table`');\n\n// Result 2\n$r2 = $db-\u003equery('SELECT * FROM `table` LIMIT 2');\n\n// Result 1 data\nif ($db-\u003enumRows($r1)) {\n\twhile ($row = $db-\u003efetchArray($r1)) {\n\t\t// Print rows\n\t\tprint_r($row);\n\t}\n}\n\n// Result 2 data\nif ($db-\u003enumRows($r2)) {\n\twhile ($row = $db-\u003efetchArray($r2)) {\n\t\t// Print rows\n\t\tprint_r($row);\n\t}\n}\n\n// Free relust 1\n$db-\u003efreeResult($r1);\n\n// Free relust 2\n$db-\u003efreeResult($r2);\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Rows, Cols num\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Do query\n$db-\u003equery('SELECT * FROM `table`');\n\n$cols = $db-\u003enumFields();\n$rows = $db-\u003enumRows();\n\n// ...\necho \"Cols: {$cols}, Rows: {$rows}\";\n\n// Free result memory\n$db-\u003efreeResult();\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Count rows\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Count all\n$count = $db-\u003ecountRows('table');\n\n// Count with condition\n$count2 = $db-\u003ecountRows('table', \"`date` = '\" . date(\"Y-m-d\") . \"'\");\n\n// ...\necho \"Count all: {$count}, Count today: {$count2}\";\n\n// More info\n/** Retrieves the number of rows from table based on certain conditions.\n * @param \tstring \t\t$table \t- Table name\n * @param \tstring \t\t$where \t- WHERE Clause\n * @return \tinteger or false\n *\n * function countRows($table, $where = NULL);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n### Execute UPDATE or INSERT queries from parameters that define the tables, fields, field values and conditions\n\n#### Array to insert\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Array data\n// [fealdname] = feald value\n$data = array();\n$data['firstname'] = 'Radovan';\n$data['surname'] = 'Janjic';\n$data['email'] = 'rade@it-radionica.com';\n// reserved values 'null', 'now()', 'curtime()', 'localtime()', 'localtime', 'utc_date()', 'utc_time()', 'utc_timestamp()'\n$data['date'] = 'now()';\n\n// $db-\u003earrayToInsert( ... ) returns insert id\n$insert_id = $db-\u003earrayToInsert('table', $data);\necho \"Last insert id is: {$insert_id}\";\n\n// More options\n/** Creates an sql string from an associate array\n * @param \tstring \t\t$table \t- Table name\n * @param \tarray \t\t$data \t- Data array Eg. $data['column'] = 'val';\n * @param \tboolean\t\t$ingore\t- INSERT IGNORE (row won't actually be inserted if it results in a duplicate key)\n * @param \tstring \t\t$duplicateupdate \t- ON DUPLICATE KEY UPDATE (The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.)\n * @return \tinsert id or false\n *\n * function arrayToInsert($table, $data, $ignore = FALSE, $duplicateupdate = NULL);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Array to insert multirow\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n \n// Array data\n// [fealdname] = feald value\n$data = array();\n \n// Data set 1\n$data[] = array(\n\t'firstname' =\u003e 'foo',\n\t'surname' =\u003e 'bar',\n\t'email' =\u003e 'hi@radovanjanjic.com',\n\t'date' =\u003e 'now()'\n);\n \n// Data set 2\n$data[] = array(\n\t'firstname' =\u003e 'baz',\n\t'surname' =\u003e 'qux',\n\t'email' =\u003e 'hi@radovanjanjic.com',\n\t'date' =\u003e 'now()'\n);\n \n// Data set ...\n \n// $db-\u003earrayToInsert( ... ) multirow returns TRUE on success\n$db-\u003earrayToInsert('table', $data);\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Array to update\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Array data\n// [fealdname] = feald value\n$data = array();\n$data['firstname'] = 'Radovan';\n$data['surname'] = 'Janjic';\n\n// Reserved values: null, now(), curtime(), localtime(), localtime, utc_date(), utc_time(), utc_timestamp()\n$data['email'] = 'null';\n$data['date'] = 'now()';\n\n\n$db-\u003earrayToUpdate('table', $data, \"`id` = {$insert_id}\");\nif ($db-\u003eaffected \u003e 0) {\n\techo \"Updated: {$db-\u003eaffected} row(s).\";\n}\n\n// More options\n/** Creates an sql string from an associate array\n * @param \tstring \t\t$table \t- Table name\n * @param \tarray \t\t$data \t- Data array Eg. $data['column'] = 'val';\n * @param \tstring \t\t$where \t- MySQL WHERE Clause\n * @param \tinteger \t$limit \t- Limit offset\n * @param \tresource \t$link \t- link identifier\n * @return \tnumber of updated rows or false\n *\n * function arrayToUpdate($table, $data, $where = NULL, $limit = 0, $link = 0);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Array to update multirow\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Array data\n// [fealdname] = feald value\n$data = array();\n\n// Data set 1\n$data[] = array(\n \n\t// Condition \n\t'id' =\u003e 1, // One of the fields has to be primary or unique key in order to update\n\t\n\t// Data to update\n\t'firstname' =\u003e 'foooo',\n\t'surname' =\u003e 'barrr'\n\t// ...\n);\n\n// Data set 2\n$data[] = array(\n \n\t// Condition \n\t'id' =\u003e 2, // One of the fields has to be primary or unique key in order to update\n\t\n\t// Data to update\n\t'firstname' =\u003e 'bazzz',\n\t'surname' =\u003e 'quxxx'\n\t// ...\n);\n\n// Data set ...\n\n// $db-\u003earrayToUpdate( ... ) multirow returns TRUE on success\n$db-\u003earrayToUpdate('table', $data);\n\n// Close connection\n$db-\u003eclose();\n```\n\n### Delete row(s)\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Delete row\n$db-\u003edeleteRow('table', \"`id` = {$insert_id}\");\n\nif ($db-\u003eaffected \u003e 0) {\n\techo \"Deleted: {$db-\u003eaffected} row(s).\";\n}\n// More options\n/** Delete row(s) from table based on certain conditions.\n * @param \tstring \t\t$table \t- Table name\n * @param \tstring \t\t$where \t- WHERE Clause\n * @param \tinteger \t$limit \t- Limit offset\n * @param \tresource \t$link \t- link identifier\n * @return \tnumber of deleted rows or false\n *\n * function deleteRow($table, $where = NULL, $limit = 0, $link = 0);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n### Operations with CSV files\n\n#### Export Table to CSV\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Export all data\n$db-\u003eexportTable2CSV('table', 'test_files/test-1.txt');\n\n// Export two or more columns\n$db-\u003eexportTable2CSV('table', 'test_files/test-2.txt', 'firstname, surname');\n\n// Export two or more columns using array\n$db-\u003eexportTable2CSV('table', 'test_files/test-3.txt', array('firstname', 'surname', 'date'));\n\n// Export all columns where id \u003c 8 and limit 1, 5\n$db-\u003eexportTable2CSV('table', 'test_files/test-4.txt', '*', 'id \u003c 8', '1,5');\n\n// More options\n/** Export table data to CSV file.\n * @param \tstring \t\t$table \t\t\t- Table name\n * @param \tstring\t\t$file\t\t\t- CSV File path\n * @param \tmixed \t\t$columns \t\t- SQL ( * or column names or array with column names)\n * @param \tstring \t\t$where \t\t\t- MySQL WHERE Clause\n * @param \tinteger \t$limit \t\t\t- Limit offset\n * @param\tstring\t\t$delimiter\t\t- COLUMNS TERMINATED BY (Default: ',')\n * @param\tstring \t\t$enclosure\t\t- OPTIONALLY ENCLOSED BY (Default: '\"')\n * @param \tstring\t\t$escape \t\t- ESCAPED BY (Default: '\\')\n * @param \tstring \t\t$newLine\t\t- New line detelimiter (Default: \\n)\n * @param \tboolean\t\t$showColumns \t- Columns names in first line\n * @return \tnumber of inserted rows or false\n *\n * function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit = 0, $delimiter = ',', $enclosure = '\"', $escape = '\\\\', $newLine = '\\n', $showColumns = TRUE);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Export query to CSV\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n$path = $db-\u003equery2CSV('select * from `table` limit 10', 'test_files/test-query2csv.csv');\necho 'Query exported to CSV file: ', $path;\n\n// Example 2\n$path = $db-\u003equery2CSV('select * from `table` limit 2,2', 'test_files/test-query2csv.csv');\n\n/** Export query to CSV file.\n * @param \tstring \t\t$sql \t\t\t- MySQL Query\n * @param \tstring\t\t$file\t\t\t- CSV File path\n * @param\tstring\t\t$delimiter\t\t- COLUMNS TERMINATED BY (Default: ',')\n * @param\tstring \t\t$enclosure\t\t- OPTIONALLY ENCLOSED BY (Default: '\"')\n * @param \tstring\t\t$escape \t\t- ESCAPED BY (Default: '\\')\n * @param \tstring \t\t$newLine\t\t- New line delimiter (Default: \\n)\n * @param \tboolean\t\t$showColumns \t- Columns names in first line\n * @return \t- File path\n *\n * function query2CSV($sql, $file, $delimiter = ',', $enclosure = '\"', $escape = '\\\\', $newLine = '\\n', $showColumns = TRUE);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Export table / export query to CSV using fputcsv\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Don't use mysql outfile\n$db-\u003emysqlOutFile = FALSE;\n\n// Table to CSV\n$db-\u003eexportTable2CSV('table', 'test_files/test-1.txt');\n\n// Query to CSV\n$path = $db-\u003equery2CSV('select * from `table` limit 10', 'test_files/test-query2csv.csv');\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Download CSV file from query\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Set as attachment and execute\n$db-\u003eattachment()-\u003equery2CSV('select * from `table`', 'test.csv');\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Import CSV to Table\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Import all data\n$db-\u003eimportCSV2Table('test_files/test-1.txt', 'table');\n\n// More options\n/** Imports CSV data to Table with possibility to update rows while import.\n * @param \tstring\t\t$file\t\t\t- CSV File path\n * @param \tstring \t\t$table \t\t\t- Table name\n * @param\tstring\t\t$delimiter\t\t- COLUMNS TERMINATED BY (Default: ',')\n * @param\tstring \t\t$enclosure\t\t- OPTIONALLY ENCLOSED BY (Default: '\"')\n * @param \tstring\t\t$escape \t\t- ESCAPED BY (Defaul: '\\')\n * @param \tinteger \t$ignore \t\t- Number of ignored rows (Default: 1)\n * @param \tarray\t\t$update \t\t- 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' =\u003e 'STR_TO_DATE(@SOME_DATE, \"%d/%m/%Y\")', 'SOME_INCREMENT' =\u003e '@SOME_INCREMENT + 1')\n * @param \tstring \t\t$getColumnsFrom\t- Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)\n * @param \tstring \t\t$newLine\t\t- New line detelimiter (Default: \\n)\n * @return \tnumber of inserted rows or false\n *\n * function importCSV2Table($file, $table, $delimiter = ',', $enclosure = '\"', $escape = '\\\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\\n');\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Import and update CSV to Table\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Import and update all data\n$db-\u003eimportUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test');\n\n// Import and update all data\n$db-\u003eimportUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test', ',', '\"', '\\\\', 1, array(), 'file', '\\r\\n');\n// More options\n/** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import.\n * @param \tstring\t\t$file\t\t\t- CSV File path\n * @param \tstring \t\t$table \t\t\t- Table name\n * @param\tstring\t\t$delimiter\t\t- COLUMNS TERMINATED BY (Default: ',')\n * @param\tstring \t\t$enclosure\t\t- OPTIONALLY ENCLOSED BY (Default: '\"')\n * @param \tstring\t\t$escape \t\t- ESCAPED BY (Defaul: '\\')\n * @param \tinteger \t$ignore \t\t- Number of ignored rows (Default: 1)\n * @param \tarray\t\t$update \t\t- 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' =\u003e 'STR_TO_DATE(@SOME_DATE, \"%d/%m/%Y\")', 'SOME_INCREMENT' =\u003e '@SOME_INCREMENT + 1')\n * @param \tstring \t\t$getColumnsFrom\t- Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file)\n * @param \tstring \t\t$newLine\t\t- New line detelimiter (Default: \\n)\n * @return \tnumber of inserted rows or false\n *\n * function importUpdateCSV2Table($file, $table, $delimiter = ',', $enclosure = '\"', $escape = '\\\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\\n');\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Create table from CSV file\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect(); \n\n$db-\u003edropTable('csv_to_table_test');\n$db-\u003ecreateTableFromCSV('test_files/countrylist.csv', 'csv_to_table_test');\n\n$db-\u003edropTable('csv_to_table_test_no_column_names');\n$db-\u003ecreateTableFromCSV('test_files/countrylist1.csv', 'csv_to_table_test_no_column_names', ',', '\"', '\\\\', 0, array(), 'generate', '\\r\\n');\n\n/** Create table from CSV file and imports CSV data to Table with possibility to update rows while import.\n * @param \tstring\t\t$file\t\t\t- CSV File path\n * @param \tstring \t\t$table \t\t\t- Table name\n * @param\tstring\t\t$delimiter\t\t- COLUMNS TERMINATED BY (Default: ',')\n * @param\tstring \t\t$enclosure\t\t- OPTIONALLY ENCLOSED BY (Default: '\"')\n * @param \tstring\t\t$escape \t\t- ESCAPED BY (Default: '\\')\n * @param \tinteger \t$ignore \t\t- Number of ignored rows (Default: 1)\n * @param \tarray\t\t$update \t\t- 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' =\u003e 'STR_TO_DATE(@SOME_DATE, \"%d/%m/%Y\")', 'SOME_INCREMENT' =\u003e '@SOME_INCREMENT + 1')\n * @param \tstring \t\t$getColumnsFrom\t- Get Columns Names from (file or generate) - this is important if there is update while inserting (Default: file)\n * @param \tstring \t\t$newLine\t\t- New line delimiter (Default: \\n)\n * @return \tnumber of inserted rows or false\n *\n * function createTableFromCSV($file, $table, $delimiter = ',', $enclosure = '\"', $escape = '\\\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\\r\\n');\n */ \n\n// Close connection\n$db-\u003eclose();\n```\n\n### Operations with XML files\n\n#### Export query to XML\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Save result as file\n$db-\u003equery2XML('select * from `table` limit 10', 'items', 'item', 'test_files/test-query2xml.csv');\n\n// Return result as XML\n$xml = $db-\u003equery2XML('select * from `table` limit 10', 'items', 'item');\n\n/** Export query to XML file or return as XML string\n * @param\tstring\t\t$query\t\t\t- mysql query\n * @param\tstring\t\t$rootElementName\t- root element name\n * @param\tstring\t\t$childElementName\t- child element name\n * @return\tstring\t\t- XML\n *\n * function query2XML($query, $rootElementName, $childElementName, $file = NULL);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Download XML file from query\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Set as attachment and execute\n$db-\u003eattachment()-\u003equery2XML('select * from `table`', 'root', 'item', 'test.xml');\n\n// Close connection\n$db-\u003eclose();\n```\n\n### Transactions\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Queries\n$queries = array();\n$queries[] = 'SELECT ...';\n$queries[] = 'INSERT ...';\n$queries[] = 'DELETE ...';\n$queries[] = '...';\n\n// Do Transaction\n$db-\u003etransaction($queries);\n\n// Get more info on: http://dev.mysql.com/doc/refman/5.0/en/commit.html\n/** Transaction\n * @param \tarray\t\t$qarr\t- Array with Queries\n * @link\thttp://dev.mysql.com/doc/refman/5.0/en/commit.html\n *\n * function transaction($qarr = array());\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n### String Search and Replace in all or defined Table Columns\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Simple\n$db-\u003estrReplace('table', 'firstname', 'search', 'replace');\n\n// Search array \u0026 Replace string\n$db-\u003estrReplace('table', 'firstname', array('search1', 'search2'), 'replace');\n\n// Search array \u0026 Replace array\n$db-\u003estrReplace('table', 'firstname', array('search1', 'search2'), array('replace1', 'replace2'));\n\n// Search array of columns (Search array \u0026 Replace array) return count of updated fielsd\n$count = $db-\u003estrReplace('table', array('firstname', 'surname'), array('search1', 'search2'), array('replace1', 'replace2'));\n\n// String multiple columns\n$db-\u003estrReplace('table', 'firstname, surname', 'search', 'replace');\n\n// You can set all columns in table as well\n$db-\u003estrReplace('table', '*', 'search', 'replace');\n\n// Whole database\n$db-\u003estrReplace('*', '*', 'search', 'replace');\n\n// More options\n/** Replace all occurrences of the search string with the replacement string in MySQL Table Column(s).\n * @param \tstring\t\t$table \t - Table name\n * @param \tmixed \t\t$columns - Search \u0026 Replace affected Table columns. An array may be used to designate multiple replacements.\n * @param \tmixed \t\t$search  - The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.\n * @param \tmixed \t\t$replace - The replacement value that replaces found search values. An array may be used to designate multiple replacements.\n * @param \tstring \t\t$where \t - WHERE Clause\n * @param \tinteger \t$limit \t - Limit offset\n * @return  integer \t- Affected rows\n *\n * function strReplace($table, $columns, $search, $replace, $where = NULL, $limit = 0);\n */\n\n// Close connection\n$db-\u003eclose();\n```\n\n### Basic Table Operation\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Copy table (with data included)\n$db-\u003ecopyTable('table', 'table_copy');\n\n// Copy table (with data included)\n$db-\u003ecopyTable('table', 'table_copy4');\n\n// Copy table structure\n$db-\u003ecopyTable('table', 'table_copy2', FALSE);\n\n// Rename table\n$db-\u003erenameTable(array('table_copy' =\u003e 'table_copy3'));\n\n// Swap table names\n$db-\u003erenameTable(array('table_copy3' =\u003e 'tmp_table', 'table_copy2' =\u003e 'table_copy3', 'tmp_table' =\u003e 'table_copy3'));\n\n// Truncate table (empty)\n$db-\u003etruncateTable('table_copy2');\n\n// Drop one table\n$db-\u003edropTable('table_copy4');\n\n// Drop multiple tables\n$db-\u003edropTable(array('table_copy3', 'table_copy2'));\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Get table columns\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Get table columns into array\n$array = $db-\u003egetColumns('table');\n\nprint_r($array);\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Get database size\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n/** Data Base size in B / KB / MB / GB / TB\n * @param \tstring\t \t$sizeIn\t\t- Size in B / KB / MB / GB / TB\n * @param \tinteger\t \t$round\t\t- Round on decimals\n * @param \tresource \t$link \t\t- Link identifier\n * @return \t- Size in B / KB / MB / GB / TB\n *\n * function getDataBaseSize($sizeIn = 'MB', $round = 2, $link = 0);\n */\n\necho 'Database size is: ', $db-\u003egetDataBaseSize('mb', 2), ' MB';\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Next AutoIncrement\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Returns next auto increment value\n$auto_increment = $db-\u003enextAutoIncrement('table');\n\necho \"Next auto increment id is: {$auto_increment}\";\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Table revision\n```php\n\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect();\n\n// Init table revision (do this only once!)\n$db-\u003einitTableRevision('rev-table');\n\n// Time to restore to ... \n$time = '2014-06-25 14:26:03';\n\n/** Create table from current revision time\n * @param \tstring\t\t$table\t\t- New table name\n * @param\tstring \t\t$rev_table\t- Revision table (origin table)\n * @param\tstring \t\t$id_field\t- Unique field name\n * @param\tdatetime\t- Revision time\n *\n * function createTableFromRevisionTime($table, $rev_table, $id_field, $time);\n */\n\t\t\n$db-\u003ecreateTableFromRevisionTime('rev-table' . '-' . $time, 'rev-table', 'id', $time);\n\n/** Restore table from current revision time\n * @param \tstring\t\t$table\t\t- New table name\n * @param\tstring \t\t$id_field\t- Unique field name\n * @param\tdatetime\t- Revision time\n *\n * function restoreTableFromRevisionTime($table, $id_field, $time);\n */\n\n$db-\u003erestoreTableFromRevisionTime('rev-table', 'id', $time);\n\n// Close connection\n$db-\u003eclose();\n```\n\n### Logging / debug\n\n#### Logging errors\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// This is useful to be TRUE!\n$db-\u003elogErrors = TRUE;\n\n// Default is FALSE, use TRUE only for debuging (security reasons!)\n$db-\u003edisplayError = TRUE;\n\n// Date / Time format for log\n$db-\u003edateFormat\t= \"Y-m-d H:i:s\"; \n\n// Log file\n$db-\u003elogFilePath = 'log-mysql.txt';\n\n// This query has error\n$db-\u003equery('SELECT * FROM `table` asfd!@#$');\n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Logging queries\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Default is FALSE, use TRUE only for debuging\n$db-\u003elogQueries = TRUE;\n\n// Log file\n$db-\u003elogFilePath = 'log-mysql.txt';\n\n// Query for this function will be logged\n$db-\u003egetColumns('table');\n\n// Query will be logged as well ...\n$db-\u003equery('SELECT * FROM `table`;');\n\n// Close connection\n$db-\u003eclose();\n```\n\n\n#### E-mail on error / die on error\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect(); \n\n// Send mail on error\n$db-\u003eemailErrors = TRUE;\n\n// Die on errors\n$db-\u003edieOnError = TRUE;\n\n// Array of emails\n$db-\u003eemailErrorsTo = array('rade@it-radionica.com');\n\n// Do first query\n$db-\u003equery(\"select * from asdf\");\n\n// This one will not be executed if first query have error and dieOnError is TRUE\n$db-\u003equery(\"select * from asdf2\"); \n\n// Close connection\n$db-\u003eclose();\n```\n\n#### Errors backtrace and debug\n\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect to host\n$db-\u003econnect();\n\n// Default is FALSE, use TRUE only for debuging (security reasons!)\n$db-\u003edisplayError = TRUE;\n\n// This query has error\n$db-\u003equery('SELECT * FROM `table` asfd!@#$');\n\n// Close connection\n$db-\u003eclose();\n\n```\n\nDisplay error example:\n\n```\nQuery fail: SELECT * FROM `table` asfd!@#$\n- Error No: 1064\n- Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '!@#$' at line 1\n- Call: Function query in C:\\xampp\\htdocs\\Git\\PHP_MySQL_wrapper\\test.php on line 29\n```\n\nDraw result / describe table / explain query:\n\n```php\n$db = MySQL_wrapper::getInstance(MySQL_HOST, MySQL_USER, MySQL_PASS, MySQL_DB);\n\n// Connect\n$db-\u003econnect(); \n\n// Draw query result data in table\n$array = $db-\u003efetchQueryToArray('SELECT * FROM `table` LIMIT 2;');\n$db-\u003edrawTable($array, 'Test table contents');\n\n/** Draw ascii table\n * @param\tarray\t$data\t- Multidimensional array of data\n * @param\tstring\t$title\t- Table header\n * @return\tvoid\n *\n * function drawTable($data, $title = NULL);\n */\n\n// Draw query execution plan in table\n$db-\u003eexplain('SELECT * FROM `table`;');\n \n// Draw information about the columns in a table\n$db-\u003edescribe('table');\n\n// Close connection\n$db-\u003eclose(); \n```\n\nDraw table output:\n\n```\n+---------------------------------------------------------------+\n|                      Test table contents                      |\n+----+-----------+---------+-----------------------+------------+\n| id | firstname | surname | email                 | date       |\n+----+-----------+---------+-----------------------+------------+\n|  1 | foo       | bar     | rade@it-radionica.com | 2014-10-02 |\n|  2 | Radovan   | Janjic  | rade@it-radionica.com | 2014-10-02 |\n+----+-----------+---------+-----------------------+------------+\n```\n\nExplain output:\n\n```\n+--------------------------------------------------------------------------------------+\n|                                 Explain MySQL Query                                  |\n+----+-------------+-------+------+---------------+-----+---------+-----+------+-------+\n| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |\n+----+-------------+-------+------+---------------+-----+---------+-----+------+-------+\n|  1 | SIMPLE      | table | ALL  |               |     |         |     |   98 |       |\n+----+-------------+-------+------+---------------+-----+---------+-----+------+-------+\n```\n\nDescribe output:\n\n```\n+------------------------------------------------------------------+\n|                               test                               |\n+-----------+--------------+------+-----+---------+----------------+\n| Field     | Type         | Null | Key | Default | Extra          |\n+-----------+--------------+------+-----+---------+----------------+\n| id        | int(11)      | NO   | PRI |         | auto_increment |\n| firstname | varchar(100) | NO   |     |         |                |\n| surname   | varchar(100) | NO   |     |         |                |\n+-----------+--------------+------+-----+---------+----------------+\n``` \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frjanjic%2Fphp_mysql_wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frjanjic%2Fphp_mysql_wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frjanjic%2Fphp_mysql_wrapper/lists"}