php创建表函数_php-使用dbDelta函数在WordPress中创建表

我需要在wordpress中为插件创建自定义表格.我遵循了一些在线文本,并创建了表,但是发现无法访问它.尝试从表中选择所有内容时,不会返回任何内容,并且尝试使用数据库浏览器插件查看表时,我收到此错误:“您的SQL语法有错误;请查看与您的MySQL服务器相对应的手册正确语法的版本,以响应插件的查询在“ FROM wp-typeEvents LIMIT 0,100’第1行附近使用”(“ SELECT SQL_CALC_FOUND_ROWS FROM wp-typeEvents LIMIT 0,100 ;;”).

简而言之,我正在尝试使用dbDelta创建表.该表已创建,但是存在某种问题,使其无法添加行或读取其内容.

我读过dbDelta可以是函数功能,所以我尝试遵守其三个黄金法则:

-将每个字段放在新行上

-在主键及其定义之间放置两个空格

-至少有一把钥匙

这是代码:

global $wpdb;

$tablename = "wp-typeEvents";

$query = "CREATE TABLE `" . $tablename . "` (

`id` mediumint(9) NOT NULL AUTO_INCREMENT,

`eventName` varchar(60) NOT NULL,

`location` varchar(60) DEFAULT '' NULL,

`price` double NOT NULL,

`description` text NOT NULL,

`paypal` varchar(60) NOT NULL,

PRIMARY KEY (`id`)

);";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

dbDelta($query);

有任何想法吗?

解决方法:

不要对表名进行硬编码,请使用$wpdb-&prefix(如其他人所述).

请勿在字段名称周围使用引号.

Note that the dbDelta function is rather picky, however. For instance:

You must put each field on its own line in your SQL statement.

You must have two spaces between the words PRIMARY KEY and the definition of your primary key.

You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.

You must not use any apostrophes or backticks around field names.

Field types must be all lowercase.

SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.

标签:wordpress,mysql,php

来源: https://codeday.me/bug/20191101/1984112.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值