加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 当返回的行为0时,为什么WordPress $wpdb-> query()响

发布时间:2020-12-13 22:00:42 所属栏目:PHP教程 来源:网络整理
导读:我在自定义函数中执行以下操作: $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.有谁
我在自定义函数中执行以下操作:

$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等.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读