gpt4 book ai didi

php - 在 Wordpress 中运行 MySQL 查询会导致 HTML 标记消失?

转载 作者:行者123 更新时间:2023-11-29 07:08:00 25 4
gpt4 key购买 nike

我们在将 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());

加载页面一次会导致空白页面。

第二次,我们看到Table 'thetesttable' already exists,这意味着我们的查询正在通过。

页面上没有其他错误或 Google Chrome 检测到的任何其他内容。

是什么导致了这个问题?

非常感谢,

贾斯蒂安迈耶

最佳答案

问题是您的查询返回 0,这在您的语句中被解释为 false:

$wpdb->query($add_query) or die(mysql_error());

来自wpdb class reference :

The function returns an integer corresponding to the number of rows affected/selected. If there is a MySQL error, the function will return FALSE. (Note: since both 0 and FALSE can be returned, make sure you use the correct comparison operator: equality == vs. identicality ===).

你应该做的是:

$result = $wpdb->query($add_query);
if ($result === false)
{
die('Could not run query');
}

编辑: 顺便说一句,还请注意,您不应像使用 wpdb 类时那样使用 mysql_error()。要获取最后一个错误,您可以使用 $wpdb->print_error();

关于php - 在 Wordpress 中运行 MySQL 查询会导致 HTML 标记消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6207738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com