Exemple #1
0
 /**
  * php < 4.0.5 only: get the content of $table as a series of INSERT
  * statements.
  * After every row, a custom callback function $handler gets called.
  *
  * Last revision 13 July 2001: Patch for limiting dump size from
  * vinay@sanisoft.com & girish@sanisoft.com
  *
  * @param   string   the current database name
  * @param   string   the current table name
  * @param   string   the 'limit' clause to use with the sql query
  * @param   string   the name of the handler (function) to use at the end
  *                   of every row. This handler must accept one parameter
  *                   ($sql_insert)
  * @param   string   the url to go back in case of error
  *
  * @return  boolean  always true
  *
  * @global  boolean  whether to use backquotes to allow the use of special
  *                   characters in database, table and fields names or not
  * @global  integer  the number of records
  * @global  integer  the current record position
  *
  * @access  private
  *
  * @see     PMA_getTableContent()
  */
 function PMA_getTableContentOld($db, $table, $add_query = '', $handler, $error_url)
 {
     global $use_backquotes;
     global $rows_cnt;
     global $current_row;
     $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
     $result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
     $current_row = 0;
     $fields_cnt = mysql_num_fields($result);
     $rows_cnt = mysql_num_rows($result);
     @set_time_limit($GLOBALS['cfgExecTimeLimit']);
     // HaRa
     while ($row = mysql_fetch_row($result)) {
         $current_row++;
         $table_list = '(';
         for ($j = 0; $j < $fields_cnt; $j++) {
             $table_list .= PMA_backquote(mysql_field_name($result, $j), $use_backquotes) . ', ';
         }
         $table_list = substr($table_list, 0, -2);
         $table_list .= ')';
         if (isset($GLOBALS['extended_ins']) && $current_row > 1) {
             $schema_insert = '(';
         } else {
             if (isset($GLOBALS['showcolumns'])) {
                 $schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes) . ' ' . PMA_htmlFormat($table_list) . ' VALUES (';
             } else {
                 $schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes) . ' VALUES (';
             }
             $is_first_row = FALSE;
         }
         for ($j = 0; $j < $fields_cnt; $j++) {
             if (!isset($row[$j])) {
                 $schema_insert .= ' NULL, ';
             } else {
                 if ($row[$j] == '0' || $row[$j] != '') {
                     $type = mysql_field_type($result, $j);
                     // a number
                     if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' || $type == 'bigint' || $type == 'timestamp') {
                         $schema_insert .= $row[$j] . ', ';
                     } else {
                         $dummy = '';
                         $srcstr = $row[$j];
                         for ($xx = 0; $xx < strlen($srcstr); $xx++) {
                             $yy = strlen($dummy);
                             if ($srcstr[$xx] == '\\') {
                                 $dummy .= '\\\\';
                             }
                             if ($srcstr[$xx] == '\'') {
                                 $dummy .= '\\\'';
                             }
                             //                            if ($srcstr[$xx] == '"')    $dummy .= '\\"';
                             if ($srcstr[$xx] == "") {
                                 $dummy .= '\\0';
                             }
                             if ($srcstr[$xx] == "\n") {
                                 $dummy .= '\\n';
                             }
                             if ($srcstr[$xx] == "\r") {
                                 $dummy .= '\\r';
                             }
                             //                            if ($srcstr[$xx] == "\x08") $dummy .= '\b';
                             //                            if ($srcstr[$xx] == "\t")   $dummy .= '\t';
                             if ($srcstr[$xx] == "") {
                                 $dummy .= '\\Z';
                             }
                             if (strlen($dummy) == $yy) {
                                 $dummy .= $srcstr[$xx];
                             }
                         }
                         $schema_insert .= "'" . $dummy . "', ";
                     }
                 } else {
                     $schema_insert .= "'', ";
                 }
             }
             // end if
         }
         // end for
         $schema_insert = ereg_replace(', $', '', $schema_insert);
         $schema_insert .= ')';
         $handler(trim($schema_insert));
         // loic1: send a fake header to bypass browser timeout if data are
         //        bufferized
         if (!empty($GLOBALS['ob_mode']) && (isset($GLOBALS['zip']) || isset($GLOBALS['bzip']) || isset($GLOBALS['gzip']))) {
             header('Expires: 0');
         }
     }
     // end while
     mysql_free_result($result);
     return TRUE;
 }
function PMA_getTableContentFast($db, $table, $add_query = '', $handler, $crlf, $error_url)
{
    global $use_backquotes;
    global $rows_cnt;
    global $current_row;
    $local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $add_query;
    $result = mysql_query($local_query);
    if ($result != FALSE) {
        $fields_cnt = mysql_num_fields($result);
        $rows_cnt = mysql_num_rows($result);
        for ($j = 0; $j < $fields_cnt; $j++) {
            $field_set[$j] = PMA_backquote(mysql_field_name($result, $j), $use_backquotes);
            $type = mysql_field_type($result, $j);
            if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' || $type == 'bigint' || $type == 'timestamp') {
                $field_num[$j] = TRUE;
            } else {
                $field_num[$j] = FALSE;
            }
        }
        if (isset($GLOBALS['showcolumns'])) {
            $fields = implode(', ', $field_set);
            $schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes) . ' (' . PMA_htmlFormat($fields) . ') VALUES (';
        } else {
            $schema_insert = 'INSERT INTO ' . PMA_backquote(PMA_htmlFormat($table), $use_backquotes) . ' VALUES (';
        }
        $search = array("", "\n", "\r", "");
        //\x08\\x09, not required
        $replace = array('\\0', '\\n', '\\r', '\\Z');
        $current_row = 0;
        @set_time_limit($GLOBALS['cfgExecTimeLimit']);
        while ($row = mysql_fetch_row($result)) {
            $current_row++;
            for ($j = 0; $j < $fields_cnt; $j++) {
                if (!isset($row[$j])) {
                    $values[] = 'NULL';
                } else {
                    if ($row[$j] == '0' || $row[$j] != '') {
                        if ($field_num[$j]) {
                            $values[] = $row[$j];
                        } else {
                            $values[] = "'" . str_replace($search, $replace, PMA_sqlAddslashes($row[$j])) . "'";
                        }
                    } else {
                        $values[] = "''";
                    }
                }
            }
            if (isset($GLOBALS['extended_ins'])) {
                if ($current_row == 1) {
                    $insert_line = $schema_insert . implode(', ', $values) . ')';
                } else {
                    $insert_line = '(' . implode(', ', $values) . ')';
                }
            } else {
                $insert_line = $schema_insert . implode(', ', $values) . ')';
            }
            unset($values);
            $handler($insert_line, $crlf);
        }
    }
    mysql_free_result($result);
    return TRUE;
}