在 WordPress 中运行 MySQL 查询会导致 HTML 标记消失?
我们在向页面添加 MySQL 查询时遇到问题。似乎每当在页面顶部运行某些内容时,要遵循的其余标记和 php 函数都不会显示/未运行。
这是导致此问题的示例查询:
global $wpdb;
$add_query = "CREATE TABLE thetesttable
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
product VARCHAR(50)
)";
$wpdb->query($add_query) or die(mysql_error());
加载页面一次会导致空白页。
第二次,我们看到表“thetesttable”已经存在
,这意味着我们的查询正在通过。
页面上没有其他错误,Google Chrome 也没有检测到任何其他内容。
什么可能导致这个问题?
非常感谢,
贾斯蒂安·迈耶
We're having an issue adding MySQL queries to our page. It seems that whenever something is run at the top of the page, the rest of the markup and php functions to follow don't show up/aren't run.
Here's a sample query that causes this issue:
global $wpdb;
$add_query = "CREATE TABLE thetesttable
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
product VARCHAR(50)
)";
$wpdb->query($add_query) or die(mysql_error());
Loading the page once results in a blank page.
The second time, we see Table 'thetesttable' already exists
, which means that our queries are getting through.
There are no other errors on the page or anything else detected by Google Chrome.
What could be causing this problem?
Many thanks,
Justian Meyer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的查询返回 0,这在您的语句中被解释为 false:
来自
wpdb< /code> 类参考
:
您应该做的是这样的:
编辑: 顺便说一句,还要注意,您不应该像使用 wpdb 类时那样使用
mysql_error()
。要获取最后一个错误,您可以使用$wpdb->print_error();
。The problem is that your query is returning 0 which is interpreted as false in your statment:
From the
wpdb
class reference:What you should do is something like:
Edit: By the way, also note that you should not use
mysql_error()
like you do when you use the wpdb class. To get the last error, you can use$wpdb->print_error();
.在所有附加评论之后,我建议添加附加查询,即
SHOW TABLES LIKE 'thetesttable';
如果上述查询没有结果,则运行
CREATE TABLE
查询,如果表存在则跳过它存在。After all additional comments, than I suggest adding additional query which is
SHOW TABLES LIKE 'thetesttable';
Run the
CREATE TABLE
query if no results from above one and skip it if table exist.