如何将CONSTRAINT与SHOW CREATE TABLE分开?

时间:2019-05-21 11:45:39

标签: php string-parsing

我想将数据库导出到sql文件,现在我有两个问题:
如何将CONSTRAINTCREATE TABLE分开?
现在的输出是这样的:

CREATE TABLE `comment` (
  `comment_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(8) unsigned NOT NULL,
  `content` varchar(300) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`comment_id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `Comment_User` FOREIGN KEY (`user_id`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

我想使它像这样(类似于phpmyadmin):

CREATE TABLE IF NOT EXISTS `comment` (
  `comment_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` int(8) UNSIGNED NOT NULL,
  `content` varchar(300) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`comment_id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `comment`
  ADD CONSTRAINT `Comment_User` FOREIGN KEY (`user_id`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE;

这是我要做的脚本:

    //loop through the tables
    foreach ($tables as $table) {
        $result = $connectdb->query('SELECT * FROM '.$table);
        $numColumns = $result->field_count;
        $comment['separation_line'] = '-- -----------'."\n\n";
        $comment['table_structure'] = $comment['separation_line'].'--'."\n".'-- Table structure for table `'.$table.'`'."\n".'--'."\n\n";
        $comment['table_dumping_data'] = '--'."\n".'-- Dumping data for table `'.$table.'`'."\n".'--'."\n\n";
        $return .= $comment['table_structure'].'DROP TABLE IF EXISTS `'.$table.'`;';
        $result2 = $connectdb->query('SHOW CREATE TABLE '.$table);
        $row2 = $result2->fetch_row();
        if ($result->num_rows > 0) {
            $return .= "\n".$row2[1].";\n\n".$comment['table_dumping_data'];
            /* Get column name */
            $column_result = $connectdb->query('SELECT * FROM '.$table);
            $column_name = $column_result->fetch_all(MYSQLI_ASSOC);
            $get_column = array();
            if (!empty($column_name)) {
                $get_column = array_keys($column_name[0]);
            }
            $columns = '`'.implode('`, `', $get_column).'`';
            /* End get column name */
            $return .= 'INSERT INTO `'.$table.'` ('.$columns.') VALUES'."\n";
        } else {
            $return .= "\n".$row2[1].";\n\n";
        }
        for ($i = 0; $i < $numColumns; $i++) {
            while ($row = $result->fetch_row()) {
                $return .= '(';
                for ($j=0; $j < $numColumns; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = str_replace("\n", '\n', $row[$j]);
                    if (isset($row[$j])) {
                        if (ctype_digit($row[$j])) {
                            $return .= $row[$j];
                        } else {
                            $return .= '`'.$row[$j].'`';
                        }
                    } else {
                        $return .= '``';
                    }
                    if ($j < ($numColumns-1)) {
                        $return.= ',';
                    }
                }
                $return .= ');'."\n";
            }
        }
        $return .= "\n";
    }

另一个小问题,当表有多于两行时,如何计算行数,然后在每行末尾添加),而不是);? 像这样:

INSERT INTO `template` (`tpl_id`, `tpl_path`) VALUES
(1, 'template-common'),
(2, 'static-css'),
(3, 'static-css');

现在它将向每行添加);

INSERT INTO `template` (`tpl_id`, `tpl_path`) VALUES
(1, 'template-common');
(2, 'static-css');
(3, 'static-css');

0 个答案:

没有答案