将表格数据插入到 Wordpress 自定义表格中。
第一步:将mysql表转成json文件
https://www.csvjson.com/sql2json
第二步:导出表格并粘贴到https://www.csvjson.com/sql2json
/**
* Continents
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `continents`
-- ----------------------------
DROP TABLE IF EXISTS `continents`;
CREATE TABLE `continents` (
`code` char(2) NOT NULL COMMENT 'Continent code',
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
-- ----------------------------
-- Records of continents
-- ----------------------------
INSERT INTO `continents` VALUES ('AF', 'Africa');
INSERT INTO `continents` VALUES ('AN', 'Antarctica');
INSERT INTO `continents` VALUES ('AS', 'Asia');
INSERT INTO `continents` VALUES ('EU', 'Europe');
INSERT INTO `continents` VALUES ('NA', 'North America');
INSERT INTO `continents` VALUES ('OC', 'Oceania');
INSERT INTO `continents` VALUES ('SA', 'South America');
INSERT INTO `continents` VALUES ('??', NULL);
步骤 3.创建目录数据
第四步,将json文件放入data目录。
第5步:将下面的代码放入插件主插件文件或anywhare eles中。
register_activation_hook(__FILE__, 'plugin_name_install_data');
function plugin_name_install_data()
{
global $wpdb;
$table_name = $wpdb->prefix . 'continents';
$contactjsondata = file_get_contents(plugin_dir_path( __FILE__ ) . 'data/continents.json');
$data = json_decode($contactjsondata, true);
foreach ($data['continents'] as $single_data) {
$item=array(
'code'=> $single_data['code'],
'name' => $single_data['name']
);
$result = $wpdb->insert($table_name, $item);
}
}