home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / libraries / export / sql.php < prev    next >
Encoding:
PHP Script  |  2004-12-29  |  23.1 KB  |  607 lines

  1. <?php
  2. /* $Id: sql.php,v 2.40 2004/12/29 09:36:25 nijel Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. error_reporting(E_ALL);
  5. /**
  6.  * Set of functions used to build SQL dumps of tables
  7.  */
  8.  
  9. /**
  10.  * Marker for comments, -- is needed for ANSI SQL.
  11.  */
  12. $GLOBALS['comment_marker'] = '-- ';
  13.  
  14. /**
  15.  * Outputs comment
  16.  *
  17.  * @param   string      Text of comment
  18.  *
  19.  * @return  bool        Whether it suceeded
  20.  */
  21. function PMA_exportComment($text) {
  22.     return PMA_exportOutputHandler($GLOBALS['comment_marker'] . $text . $GLOBALS['crlf']);
  23. }
  24.  
  25. /**
  26.  * Outputs export footer
  27.  *
  28.  * @return  bool        Whether it suceeded
  29.  *
  30.  * @access  public
  31.  */
  32. function PMA_exportFooter() {
  33.     global $crlf;
  34.  
  35.     $foot = '';
  36.  
  37.     if (isset($GLOBALS['disable_fk'])) {
  38.         $foot .=  $crlf . 'SET FOREIGN_KEY_CHECKS=1;' . $crlf;
  39.     }
  40.  
  41.     if (isset($GLOBALS['use_transaction'])) {
  42.         $foot .=  $crlf . 'COMMIT;' . $crlf;
  43.     }
  44.  
  45.     return PMA_exportOutputHandler($foot);
  46. }
  47.  
  48. /**
  49.  * Outputs export header
  50.  *
  51.  * @return  bool        Whether it suceeded
  52.  *
  53.  * @access  public
  54.  */
  55. function PMA_exportHeader() {
  56.     global $crlf;
  57.     global $cfg;
  58.  
  59.     if (PMA_MYSQL_INT_VERSION >= 40100 && isset($GLOBALS['sql_compat']) && $GLOBALS['sql_compat'] != 'NONE') {
  60.         $result = PMA_DBI_query('SET @@SQL_MODE="' . $GLOBALS['sql_compat'] . '"');
  61.         PMA_DBI_free_result($result);
  62.     }
  63.  
  64.     $head  =  $GLOBALS['comment_marker'] . 'phpMyAdmin SQL Dump' . $crlf
  65.            .  $GLOBALS['comment_marker'] . 'version ' . PMA_VERSION . $crlf
  66.            .  $GLOBALS['comment_marker'] . 'http://www.phpmyadmin.net' . $crlf
  67.            .  $GLOBALS['comment_marker'] . $crlf
  68.            .  $GLOBALS['comment_marker'] . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
  69.     if (!empty($cfg['Server']['port'])) {
  70.          $head .= ':' . $cfg['Server']['port'];
  71.     }
  72.     $head .= $crlf
  73.            .  $GLOBALS['comment_marker'] . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
  74.            .  $GLOBALS['comment_marker'] . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
  75.            .  $GLOBALS['comment_marker'] . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
  76.  
  77.     if (isset($GLOBALS['header_comment']) && !empty($GLOBALS['header_comment'])) {
  78.         $lines = explode('\n', $GLOBALS['header_comment']);
  79.         $head .= $GLOBALS['comment_marker'] . $crlf
  80.                . $GLOBALS['comment_marker'] . implode($crlf . $GLOBALS['comment_marker'], $lines) . $crlf
  81.                . $GLOBALS['comment_marker'] . $crlf;
  82.     }
  83.  
  84.     if (isset($GLOBALS['disable_fk'])) {
  85.         $head .=  $crlf . 'SET FOREIGN_KEY_CHECKS=0;' . $crlf;
  86.     }
  87.  
  88.     if (isset($GLOBALS['use_transaction'])) {
  89.         $head .=  $crlf .'SET AUTOCOMMIT=0;' . $crlf
  90.                 . 'START TRANSACTION;' . $crlf . $crlf;
  91.     }
  92.  
  93.     return PMA_exportOutputHandler($head);
  94. }
  95.  
  96. /**
  97.  * Outputs create database database
  98.  *
  99.  * @param   string      Database name
  100.  *
  101.  * @return  bool        Whether it suceeded
  102.  *
  103.  * @access  public
  104.  */
  105. function PMA_exportDBCreate($db) {
  106.     global $crlf;
  107.     if (isset($GLOBALS['drop_database'])) {
  108.         if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db) . ';' . $crlf)) return FALSE;
  109.     }
  110.     $create_query = 'CREATE DATABASE ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : $db);
  111.     if (PMA_MYSQL_INT_VERSION >= 40101) {
  112.         $collation = PMA_getDbCollation($db);
  113.         if (strpos($collation, '_')) {
  114.             $create_query .= ' DEFAULT CHARACTER SET ' . substr($collation, 0, strpos($collation, '_')) . ' COLLATE ' . $collation;
  115.         } else {
  116.             $create_query .= ' DEFAULT CHARACTER SET ' . $collation;
  117.         }
  118.     }
  119.     $create_query .= ';' . $crlf;
  120.     if (!PMA_exportOutputHandler($create_query)) return FALSE;
  121.     return PMA_exportOutputHandler('USE ' . $db . ';' . $crlf);
  122. }
  123.  
  124. /**
  125.  * Outputs database header
  126.  *
  127.  * @param   string      Database name
  128.  *
  129.  * @return  bool        Whether it suceeded
  130.  *
  131.  * @access  public
  132.  */
  133. function PMA_exportDBHeader($db) {
  134.     global $crlf;
  135.     $head = $GLOBALS['comment_marker'] . $crlf
  136.           . $GLOBALS['comment_marker'] . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
  137.           . $GLOBALS['comment_marker'] . $crlf;
  138.     return PMA_exportOutputHandler($head);
  139. }
  140.  
  141. /**
  142.  * Outputs database footer
  143.  *
  144.  * @param   string      Database name
  145.  *
  146.  * @return  bool        Whether it suceeded
  147.  *
  148.  * @access  public
  149.  */
  150. function PMA_exportDBFooter($db) {
  151.     $result = TRUE;
  152.     if (isset($GLOBALS['sql_constraints'])) {
  153.         $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']);
  154.         unset($GLOBALS['sql_constraints']);
  155.     }
  156.     return $result;
  157. }
  158.  
  159. /**
  160.  * Returns $table's CREATE definition
  161.  *
  162.  * @param   string   the database name
  163.  * @param   string   the table name
  164.  * @param   string   the end of line sequence
  165.  * @param   string   the url to go back in case of error
  166.  * @param   boolean  whether to include creation/update/check dates
  167.  *
  168.  * @return  string   resulting schema
  169.  *
  170.  * @global  boolean  whether to add 'drop' statements or not
  171.  * @global  boolean  whether to use backquotes to allow the use of special
  172.  *                   characters in database, table and fields names or not
  173.  *
  174.  * @access  public
  175.  */
  176. function PMA_getTableDef($db, $table, $crlf, $error_url, $show_dates = false)
  177. {
  178.     global $drop;
  179.     global $use_backquotes;
  180.     global $cfgRelation;
  181.     global $sql_constraints;
  182.  
  183.     $schema_create = '';
  184.     $auto_increment = '';
  185.     $new_crlf = $crlf;
  186.  
  187.  
  188.     $result = PMA_DBI_query('SHOW TABLE STATUS FROM ' . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddslashes($table) . '\'');
  189.     if ($result != FALSE) {
  190.         if (PMA_DBI_num_rows($result) > 0) {
  191.             $tmpres        = PMA_DBI_fetch_assoc($result);
  192.             if (isset($GLOBALS['auto_increment']) && !empty($tmpres['Auto_increment'])) {
  193.                 $auto_increment .= ' AUTO_INCREMENT=' . $tmpres['Auto_increment'] . ' ';
  194.             }
  195.  
  196.             if ($show_dates && isset($tmpres['Create_time']) && !empty($tmpres['Create_time'])) {
  197.                 $schema_create .= $GLOBALS['comment_marker'] . $GLOBALS['strStatCreateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Create_time'])) . $crlf;
  198.                 $new_crlf = $GLOBALS['comment_marker'] . $crlf . $crlf;
  199.             }
  200.  
  201.             if ($show_dates && isset($tmpres['Update_time']) && !empty($tmpres['Update_time'])) {
  202.                 $schema_create .= $GLOBALS['comment_marker'] . $GLOBALS['strStatUpdateTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Update_time'])) . $crlf;
  203.                 $new_crlf = $GLOBALS['comment_marker'] . $crlf . $crlf;
  204.             }
  205.  
  206.             if ($show_dates && isset($tmpres['Check_time']) && !empty($tmpres['Check_time'])) {
  207.                 $schema_create .= $GLOBALS['comment_marker'] . $GLOBALS['strStatCheckTime'] . ': ' . PMA_localisedDate(strtotime($tmpres['Check_time'])) . $crlf;
  208.                 $new_crlf = $GLOBALS['comment_marker'] . $crlf . $crlf;
  209.             }
  210.         }
  211.         PMA_DBI_free_result($result);
  212.     }
  213.  
  214.     $schema_create .= $new_crlf;
  215.  
  216.     if (!empty($drop)) {
  217.         $schema_create .= 'DROP TABLE IF EXISTS ' . PMA_backquote($table, $use_backquotes) . ';' . $crlf;
  218.     }
  219.  
  220.     // Steve Alberty's patch for complete table dump,
  221.     // Whether to quote table and fields names or not
  222.     if ($use_backquotes) {
  223.         PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 1');
  224.     } else {
  225.         PMA_DBI_query('SET SQL_QUOTE_SHOW_CREATE = 0');
  226.     }
  227.  
  228.     $result = PMA_DBI_query('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), NULL, PMA_DBI_QUERY_UNBUFFERED);
  229.     if ($result != FALSE && ($row = PMA_DBI_fetch_row($result))) {
  230.         $create_query = $row[1];
  231.         unset($row);
  232.  
  233.         // Convert end of line chars to one that we want (note that MySQL doesn't return query it will accept in all cases)
  234.         if (strpos($create_query, "(\r\n ")) {
  235.             $create_query = str_replace("\r\n", $crlf, $create_query);
  236.         } elseif (strpos($create_query, "(\n ")) {
  237.             $create_query = str_replace("\n", $crlf, $create_query);
  238.         } elseif (strpos($create_query, "(\r ")) {
  239.             $create_query = str_replace("\r", $crlf, $create_query);
  240.         }
  241.  
  242.         // Should we use IF NOT EXISTS?
  243.         if (isset($GLOBALS['if_not_exists'])) {
  244.             $create_query     = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
  245.         }
  246.  
  247.         // are there any constraints to cut out?
  248.         if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $create_query)) {
  249.  
  250.             // Split the query into lines, so we can easily handle it. We know lines are separated by $crlf (done few lines above).
  251.             $sql_lines = explode($crlf, $create_query);
  252.             $sql_count = count($sql_lines);
  253.  
  254.             // lets find first line with constraints
  255.             for ($i = 0; $i < $sql_count; $i++) {
  256.                 if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $sql_lines[$i])) break;
  257.             }
  258.  
  259.             // remove , from the end of create statement
  260.             $sql_lines[$i - 1] = preg_replace('@,$@', '', $sql_lines[$i - 1]);
  261.  
  262.             // prepare variable for constraints
  263.             if (!isset($sql_constraints)) {
  264.                 if (isset($GLOBALS['no_constraints_comments'])) {
  265.                     $sql_constraints = '';
  266.                 } else {
  267.                     $sql_constraints = $crlf . $GLOBALS['comment_marker'] .
  268.                                        $crlf . $GLOBALS['comment_marker'] . $GLOBALS['strConstraintsForDumped'] .
  269.                                        $crlf . $GLOBALS['comment_marker'] . $crlf;
  270.                 }
  271.             }
  272.  
  273.             // comments for current table
  274.             if (!isset($GLOBALS['no_constraints_comments'])) {
  275.                 $sql_constraints .= $crlf . $GLOBALS['comment_marker'] .
  276.                                     $crlf . $GLOBALS['comment_marker'] . $GLOBALS['strConstraintsForTable'] . ' ' . PMA_backquote($table) .
  277.                                     $crlf . $GLOBALS['comment_marker'] . $crlf;
  278.             }
  279.  
  280.             // let's do the work
  281.             $sql_constraints .= 'ALTER TABLE ' . PMA_backquote($table) . $crlf;
  282.  
  283.             $first = TRUE;
  284.             for($j = $i; $j < $sql_count; $j++) {
  285.                 if (preg_match('@CONSTRAINT|FOREIGN[\s]+KEY@', $sql_lines[$j])) {
  286.                     if (!$first) {
  287.                         $sql_constraints .= $crlf;
  288.                     }
  289.                     if (strpos($sql_lines[$j], 'CONSTRAINT') === FALSE) {
  290.                         $sql_constraints .= preg_replace('/(FOREIGN[\s]+KEY)/', 'ADD \1', $sql_lines[$j]);
  291.                     } else {
  292.                         $sql_constraints .= preg_replace('/(CONSTRAINT)/', 'ADD \1', $sql_lines[$j]);
  293.                     }
  294.                     $first = FALSE;
  295.                 } else {
  296.                     break;
  297.                 }
  298.             }
  299.             $sql_constraints .= ';' . $crlf;
  300.             $create_query = implode($crlf, array_slice($sql_lines, 0, $i)) . $crlf . implode($crlf, array_slice($sql_lines, $j, $sql_count - 1));
  301.             unset($sql_lines);
  302.         }
  303.         $schema_create .= $create_query;
  304.     }
  305.  
  306.     $schema_create .= $auto_increment;
  307.  
  308.     PMA_DBI_free_result($result);
  309.     return $schema_create;
  310. } // end of the 'PMA_getTableDef()' function
  311.  
  312.  
  313. /**
  314.  * Returns $table's comments, relations etc.
  315.  *
  316.  * @param   string   the database name
  317.  * @param   string   the table name
  318.  * @param   string   the end of line sequence
  319.  * @param   boolean  whether to include relation comments
  320.  * @param   boolean  whether to include column comments
  321.  * @param   boolean  whether to include mime comments
  322.  *
  323.  * @return  string   resulting comments
  324.  *
  325.  * @access  public
  326.  */
  327. function PMA_getTableComments($db, $table, $crlf, $do_relation = false, $do_comments = false, $do_mime = false)
  328. {
  329.     global $cfgRelation;
  330.     global $use_backquotes;
  331.     global $sql_constraints;
  332.  
  333.     $schema_create = '';
  334.  
  335.     if ($do_comments && $cfgRelation['commwork']) {
  336.         if (!($comments_map = PMA_getComments($db, $table))) unset($comments_map);
  337.     }
  338.  
  339.     // Check if we can use Relations (Mike Beck)
  340.     if ($do_relation && !empty($cfgRelation['relation'])) {
  341.         // Find which tables are related with the current one and write it in
  342.         // an array
  343.         $res_rel = PMA_getForeigners($db, $table);
  344.  
  345.         if ($res_rel && count($res_rel) > 0) {
  346.             $have_rel = TRUE;
  347.         } else {
  348.             $have_rel = FALSE;
  349.         }
  350.     }
  351.     else {
  352.            $have_rel = FALSE;
  353.     } // end if
  354.  
  355.     if ($do_mime && $cfgRelation['mimework']) {
  356.         if (!($mime_map = PMA_getMIME($db, $table, true))) unset($mime_map);
  357.     }
  358.  
  359.     if (isset($comments_map) && count($comments_map) > 0) {
  360.         $schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf
  361.                        . $GLOBALS['comment_marker'] . $GLOBALS['strCommentsForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
  362.         foreach ($comments_map AS $comment_field => $comment) {
  363.             $schema_create .= $GLOBALS['comment_marker'] . '  ' . PMA_backquote($comment_field, $use_backquotes) . $crlf
  364.                             . $GLOBALS['comment_marker'] . '      ' . PMA_backquote($comment, $use_backquotes) . $crlf;
  365.         }
  366.         $schema_create .= $GLOBALS['comment_marker'] . $crlf;
  367.     }
  368.  
  369.     if (isset($mime_map) && count($mime_map) > 0) {
  370.         $schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf
  371.                        . $GLOBALS['comment_marker'] . $GLOBALS['strMIMETypesForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
  372.         @reset($mime_map);
  373.         foreach ($mime_map AS $mime_field => $mime) {
  374.             $schema_create .= $GLOBALS['comment_marker'] . '  ' . PMA_backquote($mime_field, $use_backquotes) . $crlf
  375.                             . $GLOBALS['comment_marker'] . '      ' . PMA_backquote($mime['mimetype'], $use_backquotes) . $crlf;
  376.         }
  377.         $schema_create .= $GLOBALS['comment_marker'] . $crlf;
  378.     }
  379.  
  380.     if ($have_rel) {
  381.         $schema_create .= $crlf . $GLOBALS['comment_marker'] . $crlf
  382.                        . $GLOBALS['comment_marker'] . $GLOBALS['strRelationsForTable']. ' ' . PMA_backquote($table, $use_backquotes) . ':' . $crlf;
  383.         foreach ($res_rel AS $rel_field => $rel) {
  384.             $schema_create .= $GLOBALS['comment_marker'] . '  ' . PMA_backquote($rel_field, $use_backquotes) . $crlf
  385.                             . $GLOBALS['comment_marker'] . '      ' . PMA_backquote($rel['foreign_table'], $use_backquotes)
  386.                             . ' -> ' . PMA_backquote($rel['foreign_field'], $use_backquotes) . $crlf;
  387.         }
  388.         $schema_create .= $GLOBALS['comment_marker'] . $crlf;
  389.     }
  390.  
  391.     return $schema_create;
  392.  
  393. } // end of the 'PMA_getTableComments()' function
  394.  
  395. /**
  396.  * Outputs table's structure
  397.  *
  398.  * @param   string   the database name
  399.  * @param   string   the table name
  400.  * @param   string   the end of line sequence
  401.  * @param   string   the url to go back in case of error
  402.  * @param   boolean  whether to include relation comments
  403.  * @param   boolean  whether to include column comments
  404.  * @param   boolean  whether to include mime comments
  405.  *
  406.  * @return  bool     Whether it suceeded
  407.  *
  408.  * @access  public
  409.  */
  410. function PMA_exportStructure($db, $table, $crlf, $error_url, $relation = FALSE, $comments = FALSE, $mime = FALSE, $dates = FALSE) {
  411.     $formatted_table_name = (isset($GLOBALS['use_backquotes']))
  412.                           ? PMA_backquote($table)
  413.                           : '\'' . $table . '\'';
  414.     $dump = $crlf
  415.           .  $GLOBALS['comment_marker'] . '--------------------------------------------------------' . $crlf
  416.           .  $crlf . $GLOBALS['comment_marker'] . $crlf
  417.           .  $GLOBALS['comment_marker'] . $GLOBALS['strTableStructure'] . ' ' . $formatted_table_name . $crlf
  418.           .  $GLOBALS['comment_marker'] . $crlf
  419.           .  PMA_getTableDef($db, $table, $crlf, $error_url, $dates) . ';' . $crlf
  420.           .  PMA_getTableComments($db, $table, $crlf, $relation, $comments, $mime);
  421.  
  422.  
  423.     return PMA_exportOutputHandler($dump);
  424. }
  425.  
  426. /**
  427.  * Dispatches between the versions of 'getTableContent' to use depending
  428.  * on the php version
  429.  *
  430.  * @param   string      the database name
  431.  * @param   string      the table name
  432.  * @param   string      the end of line sequence
  433.  * @param   string      the url to go back in case of error
  434.  * @param   string      SQL query for obtaining data
  435.  *
  436.  * @return  bool        Whether it suceeded
  437.  *
  438.  * @global  boolean  whether to use backquotes to allow the use of special
  439.  *                   characters in database, table and fields names or not
  440.  * @global  integer  the number of records
  441.  * @global  integer  the current record position
  442.  *
  443.  * @access  public
  444.  *
  445.  * @see     PMA_getTableContentFast(), PMA_getTableContentOld()
  446.  *
  447.  * @author  staybyte
  448.  */
  449. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
  450. {
  451.     global $use_backquotes;
  452.     global $rows_cnt;
  453.     global $current_row;
  454.  
  455.     $formatted_table_name = (isset($GLOBALS['use_backquotes']))
  456.                           ? PMA_backquote($table)
  457.                           : '\'' . $table . '\'';
  458.     $head = $crlf
  459.           . $GLOBALS['comment_marker'] . $crlf
  460.           . $GLOBALS['comment_marker'] . $GLOBALS['strDumpingData'] . ' ' . $formatted_table_name . $crlf
  461.           . $GLOBALS['comment_marker'] . $crlf .$crlf;
  462.  
  463.     if (!PMA_exportOutputHandler($head)) return FALSE;
  464.  
  465.     $buffer = '';
  466.  
  467.     // analyze the query to get the true column names, not the aliases
  468.     // (this fixes an undefined index, also if Complete inserts
  469.     //  are used, we did not get the true column name in case of aliases)
  470.     $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($sql_query));
  471.  
  472.     $result      = PMA_DBI_query($sql_query, NULL, PMA_DBI_QUERY_UNBUFFERED);
  473.     if ($result != FALSE) {
  474.         $fields_cnt     = PMA_DBI_num_fields($result);
  475.  
  476.         // Get field information
  477.         $fields_meta    = PMA_DBI_get_fields_meta($result);
  478.         $field_flags    = array();
  479.         for ($j = 0; $j < $fields_cnt; $j++) {
  480.             $field_flags[$j] = PMA_DBI_field_flags($result, $j);
  481.         }
  482.  
  483.         for ($j = 0; $j < $fields_cnt; $j++) {
  484.             if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) {
  485.                 $field_set[$j] = PMA_backquote($analyzed_sql[0]['select_expr'][$j]['column'], $use_backquotes);
  486.             } else {
  487.                 $field_set[$j] = PMA_backquote($fields_meta[$j]->name, $use_backquotes);
  488.             }
  489.         }
  490.  
  491.         if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'update') {
  492.             // update
  493.             $schema_insert  = 'UPDATE ';
  494.             if (isset($GLOBALS['sql_ignore']))
  495.                 $schema_insert .= 'IGNORE ';
  496.             $schema_insert .= PMA_backquote($table, $use_backquotes) . ' SET ';
  497.         } else {
  498.             // insert or replace
  499.             if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'replace') {
  500.                 $sql_command    = 'REPLACE';
  501.             } else {
  502.                 $sql_command    = 'INSERT';
  503.             }
  504.  
  505.             // delayed inserts?
  506.             if (isset($GLOBALS['delayed'])) {
  507.                 $insert_delayed = ' DELAYED';
  508.             } else {
  509.                 $insert_delayed = '';
  510.             }
  511.  
  512.             // insert ignore?
  513.             if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'insert' && isset($GLOBALS['sql_ignore'])) {
  514.                 $insert_delayed .= ' IGNORE';
  515.             }
  516.  
  517.             // scheme for inserting fields
  518.             if (isset($GLOBALS['showcolumns'])) {
  519.                 $fields        = implode(', ', $field_set);
  520.                 $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes)
  521.                                . ' (' . $fields . ') VALUES (';
  522.             } else {
  523.                 $schema_insert = $sql_command . $insert_delayed .' INTO ' . PMA_backquote($table, $use_backquotes)
  524.                                . ' VALUES (';
  525.             }
  526.         }
  527.  
  528.         $search       = array("\x00", "\x0a", "\x0d", "\x1a"); //\x08\\x09, not required
  529.         $replace      = array('\0', '\n', '\r', '\Z');
  530.         $current_row  = 0;
  531.         $separator    = isset($GLOBALS['extended_ins']) ? ',' : ';';
  532.  
  533.         while ($row = PMA_DBI_fetch_row($result)) {
  534.             $current_row++;
  535.             for ($j = 0; $j < $fields_cnt; $j++) {
  536.                 // NULL
  537.                 if (!isset($row[$j]) || is_null($row[$j])) {
  538.                     $values[]     = 'NULL';
  539.                 // a number
  540.                 // timestamp is numeric on some MySQL 4.1
  541.                 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp') {
  542.                     $values[] = $row[$j];
  543.                 // a binary field
  544.                 // Note: with mysqli, under MySQL 4.1.3, we get the flag
  545.                 // "binary" for those field types (I don't know why)
  546.                 } else if (stristr($field_flags[$j], 'BINARY')
  547.                         && isset($GLOBALS['hexforbinary'])
  548.                         && $fields_meta[$j]->type != 'datetime'
  549.                         && $fields_meta[$j]->type != 'date'
  550.                         && $fields_meta[$j]->type != 'time'
  551.                         && $fields_meta[$j]->type != 'timestamp'
  552.                        ) {
  553.                     // empty blobs need to be different, but '0' is also empty :-(
  554.                     if (empty($row[$j]) && $row[$j] != '0') {
  555.                         $values[] = '\'\'';
  556.                     } else {
  557.                         $values[] = '0x' . bin2hex($row[$j]);
  558.                     }
  559.                 // something else -> treat as a string
  560.                 } else {
  561.                     $values[] = '\'' . str_replace($search, $replace, PMA_sqlAddslashes($row[$j])) . '\'';
  562.                 } // end if
  563.             } // end for
  564.  
  565.             // should we make update?
  566.             if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'update') {
  567.  
  568.                 $insert_line = $schema_insert;
  569.                 for ($i = 0; $i < $fields_cnt; $i++) {
  570.                     if ($i > 0) {
  571.                         $insert_line .= ', ';
  572.                     }
  573.                     $insert_line .= $field_set[$i] . ' = ' . $values[$i];
  574.                 }
  575.  
  576.                 $insert_line .= ' WHERE ' . PMA_getUvaCondition($result, $fields_cnt, $fields_meta, $row);
  577.  
  578.             } else {
  579.  
  580.                 // Extended inserts case
  581.                 if (isset($GLOBALS['extended_ins'])) {
  582.                     if ($current_row == 1) {
  583.                         $insert_line  = $schema_insert . implode(', ', $values) . ')';
  584.                     } else {
  585.                         $insert_line  = '(' . implode(', ', $values) . ')';
  586.                     }
  587.                 }
  588.                 // Other inserts case
  589.                 else {
  590.                     $insert_line      = $schema_insert . implode(', ', $values) . ')';
  591.                 }
  592.             }
  593.             unset($values);
  594.  
  595.             if (!PMA_exportOutputHandler(($current_row == 1 ? '' : $separator . $crlf) . $insert_line)) return FALSE;
  596.  
  597.         } // end while
  598.         if ($current_row > 0) {
  599.             if (!PMA_exportOutputHandler(';' . $crlf)) return FALSE;
  600.         }
  601.     } // end if ($result != FALSE)
  602.     PMA_DBI_free_result($result);
  603.  
  604.     return TRUE;
  605. } // end of the 'PMA_exportData()' function
  606. ?>
  607.