Provide zblog template_zblog them_wordpress template download and customization

The code and steps for creating a new data table in WordPress

Tianxing Studio 2022-03-04 16:42 Wordpress Tutorial one thousand one hundred and eighty-two 0 Comments


When customizing for a customer today wordpress A new data table is created in it. I really don't want to see the source code. I went directly to a search engine and searched "Wordpress New Data Table", excluding csdn and "it1352" website Later, the search results were similar, most of them were copied ”I love boiled fish "

Then I directly copied the code of boiled fish. After testing, I found that the new data table was not successful. Comprehensive reference to other search results is also unsuccessful.

We can only give up the habit of writing code for search engines, and look at the source code of the new data table created by wp in the "wp admin/includes/schema. php" file.

After comparison, I think it is the character set problem of the database?

The code of boiled fish is as follows:

 function weixin_robot_texts_crate_table() {     global $wpdb;     $table_name = $wpdb->prefix .  "weixin_robot_texts";     if($wpdb->get_var("show tables like '$table_name'") !=  $weixin_robot_texts_table) {         $sql = "CREATE TABLE " .  $table_name .  " (               `id` bigint(20) NOT NULL AUTO_INCREMENT,               `keyword` varchar(255) CHARACTER SET utf8 NOT NULL,               `reply` text CHARACTER SET utf8 NOT NULL,               `status` int(1) NOT NULL DEFAULT '1',               `time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',               PRIMARY KEY (`id`),               UNIQUE KEY `keyword` (`keyword`)             ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;         ";     require_once(ABSPATH . 'wp-admin/includes/upgrade.php');     dbDelta($sql); }} register_activation_hook( __FILE__,'weixin_robot_texts_crate_table');

This character set is written directly. The wp source code reads as follows:

 $charset_collate = $wpdb->get_charset_collate();

Then I changed the code:

 function my_table_crate_table() {     global $wpdb;     $charset_collate = $wpdb->get_charset_collate();     $table_name = $wpdb->prefix .  "my_table_name";     if($wpdb->get_var("show tables like '$table_name'") !=  $table_name) {         $sql = "CREATE TABLE " .  $table_name .  " (               `id` bigint(20) NOT NULL AUTO_INCREMENT,               `keyword` varchar(255) CHARACTER SET utf8 NOT NULL,               `reply` text CHARACTER SET utf8 NOT NULL,               `status` int(1) NOT NULL DEFAULT '1',               `time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',                PRIMARY KEY (`id`),               UNIQUE KEY `keyword` (`keyword`)             ) '.$ charset_collate.';         ";     require_once(ABSPATH . 'wp-admin/includes/upgrade.php');     dbDelta($sql); }} register_activation_hook(__FILE__, 'my_table_crate_table');

It really succeeded

PS: At the end, I still want to complain about the results of the search engine. I will not talk about the cancer of csdn, Alibaba Cloud Tencent Cloud use key word Creating a bunch of related spicy chicken pages is even more immoral. Most websites are copied and pasted. What's more wonderful is that the content of it1352 doesn't matter if it's hot chicken. When browsing hot chicken, you should also pay attention to their hot chicken official account. It's really a pile of dung making machines


Can't find a tutorial that can solve your problem?

You can try to search or ask questions directly online. We also provide charging technical support. If you need it, you can contact us online.

Online questions Online Service

welcome you Comment: Cancel Reply

 Please fill in the verification code
  • Latest articles
  • Hot article ranking
  • Most Comments
Label aggregation
  • Sign in
  • Registered account Forgot your password?
  • register
  • Already have an account? Direct login Forgot your password?
  • Social account login