Skip to content

Commit d543de5

Browse files
author
uzi88
committed
FIXED: Limit with offset bug in query2CSV
1 parent f4ef632 commit d543de5

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

MySQL_wrapper.class.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ function importCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $esc
408408
*/
409409
function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit = 0, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE, $link = 0) {
410410
$this->link = $link ? $link : $this->link;
411-
$fh = fopen($file, 'w') or ($this->logErrors) ? $this->log("ERROR", "Can't create CSV file.") : FALSE;
411+
$fh = fopen($file, 'w') or ($this->logErrors) ? $this->log("ERROR", "Can't create CSV file: {$file}") : FALSE;
412412
fclose($fh);
413413
$file = realpath($file);
414414
unlink($file);
@@ -459,7 +459,7 @@ function exportTable2CSV($table, $file, $columns = '*', $where = NULL, $limit =
459459
*/
460460
function query2CSV($sql, $file, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE, $link = 0) {
461461
$this->link = $link ? $link : $this->link;
462-
$fh = fopen($file, 'w') or ($this->logErrors) ? $this->log("ERROR", "Can't create CSV file.") : FALSE;
462+
$fh = fopen($file, 'w') or ($this->logErrors) ? $this->log("ERROR", "Can't create CSV file: {$file}") : FALSE;
463463
fclose($fh);
464464
$file = realpath($file);
465465
unlink($file);
@@ -476,11 +476,14 @@ function query2CSV($sql, $file, $delimiter = ',', $enclosure = '"', $escape = '\
476476
foreach ($columns as $k => $v) {
477477
$tableColumnsArr[] = "'{$k}' AS `{$k}`";
478478
}
479-
$columnsSQL = "SELECT " . implode(', ', $tableColumnsArr) . " UNION ALL ";
479+
$columnsSQL = "SELECT " . implode(', ', $tableColumnsArr);
480+
} else {
481+
// No results for this query
482+
return 0;
480483
}
481484
}
482485
// Final query
483-
$sql = (($showColumns) ? $columnsSQL : NULL) . "{$sql} " .
486+
$sql = (($showColumns && isset($columnsSQL)) ? "SELECT * FROM ( ( " . $columnsSQL . " ) UNION ALL ( {$sql} ) ) `a` " : "{$sql} ") .
484487
"INTO OUTFILE '{$this->escape($file)}' " .
485488
"FIELDS TERMINATED BY '{$delimiter}' " .
486489
"OPTIONALLY ENCLOSED BY '{$enclosure}' " .

example.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@
472472
// function query2CSV($sql, $file, $delimiter = ',', $enclosure = '"', $escape = '\\', $newLine = '\n', $showColumns = TRUE, $link = 0)
473473
$path = $db->query2CSV('select * from `table` limit 10', 'test-query2csv.csv');
474474
echo '<hr /><pre>Query exported to CSV file: ', $path, '</pre>';
475+
476+
// example 2
477+
$path = $db->query2CSV('select * from `table` limit 2,2', 'test-query2csv.csv');
475478
// Close connection
476479
$db->close();
477480
///////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)