@@ -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 }' " .
0 commit comments