php - 为什么当返回的行为 0 时 Wordpress $wpdb->query() 响应为 1

标签 php wordpress

我在自定义函数中执行以下操作:

    $exists = $wpdb->query($wpdb->prepare('
    SELECT COUNT(*)
    FROM wp_%d_gdsr_data_article
    WHERE post_id = %d

', $blog_id, $post_id));

即使查询没有返回任何行,$exists 的计算结果也是 1。此外,var_dump($wpdb->queries) 产生 NULL。有人知道这里发生了什么吗?

谢谢,

最佳答案

来自documentation :

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 ===).

查询返回 1 行,因此 query() 函数返回 1 - 并且对于您在其中发布的查询将始终返回 1您的问题,即使 COUNT 选择的行数为 0。使用 get_varget_rowget_results 按照 TheDeadMedic 的建议获取查询的实际结果,可能是 012

关于php - 为什么当返回的行为 0 时 Wordpress $wpdb->query() 响应为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2989845/

相关文章:

php - 如何在 PHPUnit 中模拟 MongoClient

wordpress - 在WordPress中将短代码转换为YouTube链接

php - Laravel - 迁移,表结构修改 - 正确方法

php - Laravel 策略 - 如何将多个参数传递给函数

php - 旋转并将图像的旋转存储在 php/mysql 网站中?

php - Wordpress 本地克隆无法正常工作

php - 在wordpress的cherry框架中自动更改css

javascript - 您已在此页面上多次包含 Google Maps API

javascript - 如何通过Javascript检索wordpress中的当前用户?

java - 如何翻译不同语言的城市名称