Skip to content

Commit 1c0d5c9

Browse files
author
uzi88
committed
Added: Export query to CSV file & Table 2 CSV FIX
- Added: Export query to CSV file - FIX: Table 2 CSV now returns file path - Examples update
1 parent f5d94f6 commit 1c0d5c9

File tree

2 files changed

+121
-61
lines changed

2 files changed

+121
-61
lines changed

MySQL_wrapper.class.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ function importCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $esc
359359
* @param string $newLine - New line delimiter (Default: \n)
360360
* @param boolean $showColumns - Columns names in first line
361361
* @param resource $link - Link identifier
362-
* @return number of inserted rows or false
362+
* @return - File path
363363
*/
364364
function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit = 0, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE, $link = 0){
365365
$this->link = $link ? $link : $this->link;
@@ -398,7 +398,49 @@ function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit =
398398
"ESCAPED BY '{$this->escape($escape)}' " .
399399
"LINES TERMINATED BY '{$newLine}' " .
400400
"FROM `{$table}`" . ($where ? " WHERE {$where}" : NULL) . ($limit ? " LIMIT {$limit}" : NULL) . ";";
401-
return ($this->query($sql, $this->link)) ? TRUE : FALSE;
401+
return ($this->query($sql, $this->link)) ? $file : FALSE;
402+
}
403+
404+
/** Export query to CSV file.
405+
* @param string $sql - MySQL Query
406+
* @param string $file - CSV File path
407+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
408+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
409+
* @param string $escape - ESCAPED BY (Default: '\')
410+
* @param string $newLine - New line delimiter (Default: \n)
411+
* @param boolean $showColumns - Columns names in first line
412+
* @param resource $link - Link identifier
413+
* @return - File path
414+
*/
415+
function query2CSV($sql, $file, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE, $link = 0) {
416+
$this->link = $link ? $link : $this->link;
417+
$fh = fopen($file, 'w') or ($this->logErrors) ? $this->log("ERROR", "Can't create CSV file.") : FALSE;
418+
fclose($fh);
419+
$file = realpath($file);
420+
unlink($file);
421+
// Remove ; from end of query
422+
$sql = rtrim(trim($sql), ';');
423+
// Prepare SQL for column names
424+
if ($showColumns) {
425+
$r = $this->query(preg_replace('/(limit)\s\d(((\,\s)|(\s\,\s)|(\s\,))\d)?$/i', 'LIMIT 1', $sql), $this->link);
426+
if ($r !== FALSE && $this->affected > 0) {
427+
$columns = $this->fetchArray($r);
428+
$this->freeResult($r);
429+
$tableColumnsArr = array();
430+
foreach ($columns as $k => $v) {
431+
$tableColumnsArr[] = "'{$k}' AS `{$k}`";
432+
}
433+
$columnsSQL = "SELECT " . implode(', ', $tableColumnsArr) . " UNION ALL ";
434+
}
435+
}
436+
// Final query
437+
$sql = (($showColumns) ? $columnsSQL : NULL) . "{$sql} " .
438+
"INTO OUTFILE '{$this->escape($file)}' " .
439+
"FIELDS TERMINATED BY '{$delimiter}' " .
440+
"OPTIONALLY ENCLOSED BY '{$enclosure}' " .
441+
"ESCAPED BY '{$this->escape($escape)}' " .
442+
"LINES TERMINATED BY '{$newLine}';";
443+
return ($this->query($sql, $this->link)) ? $file : FALSE;
402444
}
403445

404446
/** Rename table(s)

example.php

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
$db->close();
6262
///////////////////////////////////////////////////////////////////////////////////////////
6363

64-
6564
// Example 2
6665
// Connection example
6766
///////////////////////////////////////////////////////////////////////////////////////////
@@ -132,7 +131,6 @@
132131
$db->close();
133132
///////////////////////////////////////////////////////////////////////////////////////////
134133

135-
136134
// Example 5
137135
// Faster select exmaple (fetch query to array)
138136
///////////////////////////////////////////////////////////////////////////////////////////
@@ -144,7 +142,6 @@
144142
$db->close();
145143
///////////////////////////////////////////////////////////////////////////////////////////
146144

147-
148145
// Exmaple 6
149146
// Multi results
150147
///////////////////////////////////////////////////////////////////////////////////////////
@@ -180,7 +177,6 @@
180177
$db->close();
181178
///////////////////////////////////////////////////////////////////////////////////////////
182179

183-
184180
// Example 7
185181
// Rows, Cols num
186182
///////////////////////////////////////////////////////////////////////////////////////////
@@ -199,7 +195,6 @@
199195
$db->close();
200196
///////////////////////////////////////////////////////////////////////////////////////////
201197

202-
203198
// Example 8
204199
// Count rows
205200
///////////////////////////////////////////////////////////////////////////////////////////
@@ -224,7 +219,6 @@
224219
$db->close();
225220
///////////////////////////////////////////////////////////////////////////////////////////
226221

227-
228222
// Example 9
229223
// Array to insert
230224
///////////////////////////////////////////////////////////////////////////////////////////
@@ -258,7 +252,6 @@
258252
$db->close();
259253
///////////////////////////////////////////////////////////////////////////////////////////
260254

261-
262255
// Example 10
263256
// Next AutoIncrement
264257
///////////////////////////////////////////////////////////////////////////////////////////
@@ -340,6 +333,54 @@
340333
///////////////////////////////////////////////////////////////////////////////////////////
341334

342335
// Example 14
336+
// Basic Table Operation
337+
///////////////////////////////////////////////////////////////////////////////////////////
338+
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
339+
// Connect
340+
$db->connect();
341+
342+
// Rename table
343+
$db->renameTable(array('old_table_name' => 'new_table_name'));
344+
// Swap table names
345+
$db->renameTable(array('table1' => 'tmp_table', 'table2' => 'table1', 'tmp_table' => 'table1'));
346+
347+
// Copy table (with data included)
348+
$db->copyTable('table', 'table_copy');
349+
// Copy table structure
350+
$db->copyTable('table', 'table_copy2', FALSE);
351+
352+
// Truncate table (empty)
353+
$db->truncateTable('table_copy2');
354+
355+
// Drop one table
356+
$db->dropTable('table_copy2');
357+
// Drop multiple tables
358+
$db->dropTable(array('table_copy', 'table_copy2'));
359+
360+
// Close connection
361+
$db->close();
362+
///////////////////////////////////////////////////////////////////////////////////////////
363+
364+
// Example 15
365+
// Get database size
366+
///////////////////////////////////////////////////////////////////////////////////////////
367+
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
368+
369+
// Connect
370+
$db->connect();
371+
/** Data Base size in B / KB / MB / GB / TB
372+
* @param string $sizeIn - Size in B / KB / MB / GB / TB
373+
* @param integer $round - Round on decimals
374+
* @param resource $link - Link identifier
375+
* @return - Size in B / KB / MB / GB / TB
376+
*/
377+
// function getDataBaseSize($sizeIn = 'MB', $round = 2, $link = 0)
378+
echo '<hr /><pre>Database size is: ', $db->getDataBaseSize('mb', 2), ' MB</pre>';
379+
// Close connection
380+
$db->close();
381+
///////////////////////////////////////////////////////////////////////////////////////////
382+
383+
// Example 16
343384
// Loging queries and errors
344385
///////////////////////////////////////////////////////////////////////////////////////////
345386
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
@@ -358,7 +399,7 @@
358399
$db->close();
359400
///////////////////////////////////////////////////////////////////////////////////////////
360401

361-
// Example 15
402+
// Example 17
362403
// Export Table to CSV
363404
///////////////////////////////////////////////////////////////////////////////////////////
364405
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
@@ -390,7 +431,32 @@
390431
$db->close();
391432
///////////////////////////////////////////////////////////////////////////////////////////
392433

393-
// Example 16
434+
// Example 18
435+
// Query to CSV
436+
///////////////////////////////////////////////////////////////////////////////////////////
437+
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
438+
439+
// Connect
440+
$db->connect();
441+
/** Export query to CSV file.
442+
* @param string $sql - MySQL Query
443+
* @param string $file - CSV File path
444+
* @param string $delimiter - COLUMNS TERMINATED BY (Default: ',')
445+
* @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"')
446+
* @param string $escape - ESCAPED BY (Default: '\')
447+
* @param string $newLine - New line delimiter (Default: \n)
448+
* @param boolean $showColumns - Columns names in first line
449+
* @param resource $link - Link identifier
450+
* @return - File path
451+
*/
452+
// function query2CSV($sql, $file, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE, $link = 0)
453+
$path = $db->query2CSV('select * from `table` limit 10', 'test-query2csv.csv');
454+
echo '<hr /><pre>Query exported to CSV file: ', $path, '</pre>';
455+
// Close connection
456+
$db->close();
457+
///////////////////////////////////////////////////////////////////////////////////////////
458+
459+
// Example 19
394460
// Import CSV to Table
395461
///////////////////////////////////////////////////////////////////////////////////////////
396462
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
@@ -417,7 +483,7 @@
417483
$db->close();
418484
///////////////////////////////////////////////////////////////////////////////////////////
419485

420-
// Example 17
486+
// Example 20
421487
// Import CSV to Table
422488
///////////////////////////////////////////////////////////////////////////////////////////
423489
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
@@ -439,7 +505,7 @@
439505
// Close connection
440506
$db->close();
441507

442-
// Example 18
508+
// Example 21
443509
// String Replace Table Columns
444510
///////////////////////////////////////////////////////////////////////////////////////////
445511
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
@@ -471,51 +537,3 @@
471537
// function strReplace($table, $columns, $search, $replace, $where = NULL, $limit = 0, $link = 0)
472538
// Close connection
473539
$db->close();
474-
475-
// Example 20
476-
// Basic Table Operation
477-
///////////////////////////////////////////////////////////////////////////////////////////
478-
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
479-
// Connect
480-
$db->connect();
481-
482-
// Rename table
483-
$db->renameTable(array('old_table_name' => 'new_table_name'));
484-
// Swap table names
485-
$db->renameTable(array('table1' => 'tmp_table', 'table2' => 'table1', 'tmp_table' => 'table1'));
486-
487-
// Copy table (with data included)
488-
$db->copyTable('table', 'table_copy');
489-
// Copy table structure
490-
$db->copyTable('table', 'table_copy2', FALSE);
491-
492-
// Truncate table (empty)
493-
$db->truncateTable('table_copy2');
494-
495-
// Drop one table
496-
$db->dropTable('table_copy2');
497-
// Drop multiple tables
498-
$db->dropTable(array('table_copy', 'table_copy2'));
499-
500-
// Close connection
501-
$db->close();
502-
503-
///////////////////////////////////////////////////////////////////////////////////////////
504-
505-
// Example 21
506-
// Get database size
507-
///////////////////////////////////////////////////////////////////////////////////////////
508-
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
509-
510-
// Connect
511-
$db->connect();
512-
/** Data Base size in B / KB / MB / GB / TB
513-
* @param string $sizeIn - Size in B / KB / MB / GB / TB
514-
* @param integer $round - Round on decimals
515-
* @param resource $link - Link identifier
516-
* @return - Size in B / KB / MB / GB / TB
517-
*/
518-
// function getDataBaseSize($sizeIn = 'MB', $round = 2, $link = 0)
519-
echo '<hr /><pre>Database size is: ', $db->getDataBaseSize('mb', 2), ' MB</pre>';
520-
// Close connection
521-
$db->close();

0 commit comments

Comments
 (0)