我在自定义函数中执行以下操作:
$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_var
、get_row
或 get_results
按照 TheDeadMedic 的建议获取查询的实际结果,可能是 0
、1
、2
等
关于php - 为什么当返回的行为 0 时 Wordpress $wpdb->query() 响应为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2989845/