diff --git a/ChangeLog b/ChangeLog index e4468b57d..65ff61774 100755 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,9 @@ $Source$ * libraries/sqlparser.lib.php3: Fixed XSS problem. * tbl_properties.inc.php3, libraries/functions.js: Alert user when not specied length for CHAR/VARCHAR fields. + * libraries/export/{csv,latex,xml}.php3: Use correct field names when + exporting custom SQL (also saves one SQL query for export) (fixes bug + #755386). 2003-06-24 Marc Delisle * sql.php3, bug 759568, row count, DISTINCT and MySQL 4 diff --git a/libraries/export/csv.php3 b/libraries/export/csv.php3 index 5918c86a2..60123eb1c 100644 --- a/libraries/export/csv.php3 +++ b/libraries/export/csv.php3 @@ -109,30 +109,27 @@ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { global $enclosed; global $escaped; + // Gets the data from the database + $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url); + $fields_cnt = mysql_num_fields($result); // If required, get fields name at the first line if (isset($GLOBALS['showcsvnames']) && $GLOBALS['showcsvnames'] == 'yes') { $schema_insert = ''; - $local_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db); - $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url); - while ($row = PMA_mysql_fetch_array($result)) { - if ($enc_by == '') { - $schema_insert .= $row['Field']; + for ($i = 0; $i < $fields_cnt; $i++) { + if ($enclosed == '') { + $schema_insert .= mysql_field_name($result, $i); } else { - $schema_insert .= $enc_by - . str_replace($enc_by, $esc_by . $enc_by, $row['Field']) - . $enc_by; + $schema_insert .= $enclosed + . str_replace($enclosed, $escaped . $enclosed, mysql_field_name($result, $i)) + . $enclosed; } - $schema_insert .= $sep; - } // end while + $schema_insert .= $separator; + } // end for $schema_insert =trim(substr($schema_insert, 0, -1)); if (!PMA_exportOutputHandler($schema_insert . $add_character)) return FALSE; } // end if - // Gets the data from the database - $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url); - $fields_cnt = mysql_num_fields($result); - // Format the data $i = 0; while ($row = PMA_mysql_fetch_row($result)) { diff --git a/libraries/export/latex.php3 b/libraries/export/latex.php3 index 00e49cf74..a74faf8b2 100644 --- a/libraries/export/latex.php3 +++ b/libraries/export/latex.php3 @@ -100,21 +100,16 @@ function PMA_exportDBCreate($db) { * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { - - $local_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db); - $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url); - for ($i = 0; $row = PMA_mysql_fetch_array($result, MYSQL_ASSOC); $i++) { - $columns[$i] = $row['Field']; - } - $columns_cnt = count($columns); - unset($i); - unset($local_query); - mysql_free_result($result); - $tex_escape = array("$", "%", "{", "}", "&", "#", "_", "^"); $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url); + $columns_cnt = mysql_num_fields($result); + for ($i = 0; $i < $columns_cnt; $i++) { + $columns[$i] = mysql_field_name($result, $i); + } + unset($i); + $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . $crlf . '%' . $crlf . '\\begin{table} ' . $crlf . ' \\begin{longtable}{|'; diff --git a/libraries/export/xml.php3 b/libraries/export/xml.php3 index 8a63d126c..a2c313067 100644 --- a/libraries/export/xml.php3 +++ b/libraries/export/xml.php3 @@ -106,16 +106,14 @@ function PMA_exportDBCreate($db) { * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { - $local_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db); - $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url); - for ($i = 0; $row = PMA_mysql_fetch_array($result, MYSQL_ASSOC); $i++) { - $columns[$i] = $row['Field']; - } - $columns_cnt = count($columns); - unset($i); - mysql_free_result($result); - $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url); + + $columns_cnt = mysql_num_fields($result); + for ($i = 0; $i < $columns_cnt; $i++) { + $columns[$i] = mysql_field_name($result, $i); + } + unset($i); + $buffer = ' ' . $crlf; if (!PMA_exportOutputHandler($buffer)) return FALSE;