Php 如何制作mysql表的副本,添加一些列并删除一些列,更改顺序并保留与新表相关的任何数据?
我希望wordpress用户能够改变数据库表的结构,同时在保持不变的列中保留数据 到目前为止,我的策略是: 使用新结构创建一个名为table_temp的新表。 对于新表中存在的旧表中的每一列,循环 通过旧表的行并将值插入到新表中 表列。 删除旧表并将新表重命名为旧表 桌子 第二步是我出错的地方,所以我还没有考虑第三步。第一列工作正常,但插入第二个保留列的值从插入第一列的最后一个值后的行开始Php 如何制作mysql表的副本,添加一些列并删除一些列,更改顺序并保留与新表相关的任何数据?,php,mysql,wordpress,Php,Mysql,Wordpress,我希望wordpress用户能够改变数据库表的结构,同时在保持不变的列中保留数据 到目前为止,我的策略是: 使用新结构创建一个名为table_temp的新表。 对于新表中存在的旧表中的每一列,循环 通过旧表的行并将值插入到新表中 表列。 删除旧表并将新表重命名为旧表 桌子 第二步是我出错的地方,所以我还没有考虑第三步。第一列工作正常,但插入第二个保留列的值从插入第一列的最后一个值后的行开始 function update_table($_POST) { global $wpdb; //coll
function update_table($_POST) {
global $wpdb;
//collect the updated table's metadata from the $_POST
$slug = $_POST[slug].'_temp';
$WB_table = $_POST[slug];
unset($_POST[slug]);
unset($_POST[tblAppendGrid_rowOrder]);
unset($_POST[status]);
// create the new temp table
$wpdb->query("
CREATE TABLE $slug (
id INT (6) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
) ENGINE=MyISAM;
");
//remove reference to the old meta in WB_table _meta
$wpdb->delete( 'WB_table_meta', array( 'WB_table' => $WB_table ) );
// add the new meta to WB_table _meta
// loop through the $_POST array
for($i=1;$i<=count($_POST)/5;$i++) {
// pull the array elemnts from $_POST
$name = $_POST['tblAppendGrid_name_'.$i];
$display = $_POST['tblAppendGrid_display_'.$i];
$type = $_POST['tblAppendGrid_type_'.$i];
$maxlength = $_POST['tblAppendGrid_maxlength_'.$i];
$width = $_POST['tblAppendGrid_width_'.$i];
// Add column names to the new table
$wpdb->query("ALTER TABLE ".$slug." ADD ".$name." text(160)");
// Add meta to WB_table_meta
$wpdb->insert('WB_table_meta',
array(
'WB_table' => $WB_table,
'name' => $name,
'display' => $display,
'type' => $type,
'maxlength' => $maxlength,
'width' => $width),
array('%s','%s','%s','%s','%s','%d','%d')
);
//copy data to the new table from the old table if the column exists in the new table
if ($wpdb->query("SELECT ".$name." from ".$WB_table."", A_ARRAY) == true) {
$rows = $wpdb->get_col("SELECT ".$name." from ".$WB_table."");
foreach($rows as $row){
$wpdb->query("INSERT INTO $slug ($name) VALUES ('$row')");
$wpdb->show_errors();
} //for each
} // if
} // for loop
} // function update_table
谢谢VMai。你的建议很有效。下面的代码生成了具有正确字段的新表
// build the column string
if ($wpdb->query("SELECT ".$name." from ".$WB_table."", A_ARRAY) == true) {
$cols .= $name.',';
} // if
} // for loop
$col = substr($cols, 0, -1);
//copy data to the new table from the old table if the column exists in the new table
$wpdb->query("
INSERT INTO ".$slug."
(".$col.")
SELECT ".$col."
FROM ".$WB_table."
");
$wpdb->show_errors();
我想还有更糟糕的想法。一下子想不出来。为什么不简单地使用后跟重命名表的方法呢?如果要单独创建表,可以使用。因为让用户能够改变数据库表的结构是个坏主意。他们会打破它的。
// build the column string
if ($wpdb->query("SELECT ".$name." from ".$WB_table."", A_ARRAY) == true) {
$cols .= $name.',';
} // if
} // for loop
$col = substr($cols, 0, -1);
//copy data to the new table from the old table if the column exists in the new table
$wpdb->query("
INSERT INTO ".$slug."
(".$col.")
SELECT ".$col."
FROM ".$WB_table."
");
$wpdb->show_errors();