【问题标题】:$wpdb - what does it return on fail?$wpdb - 失败时返回什么?
【发布时间】:2023-03-13 04:53:01
【问题描述】:

我不确定这个问题是 WordPress 特定的还是与 mySQL 更相关。我试图找出如果与数据库的事务失败会返回什么。 在以下场景中,我正在更新一行。如果没有更改任何值,则返回 false。如果进行了更改,则返回 true。如何判断交易是否失败?

$result = $wpdb->update($this->table_name, $dbfields, $where);
if($result == false)//do fail - this is not really a fail!
if($result == true)//do success

任何指针表示赞赏。

【问题讨论】:

    标签: php mysql wordpress ezsql


    【解决方案1】:

    看看wp-includes\wp-db.php。 wpdb 的更新函数的头部 cmets 说:

     * @return int|false The number of rows updated, or false on error.
    

    所以,我怀疑您想找出false(表示失败的布尔值)和0(表示未返回任何行的整数)之间的区别。

    如果你用==比较,false0是相等的。因此,您需要使用=== 运算符来检查您处理的是布尔值false 还是整数0

    那么,试试这些:

    if ($result === false) // Fail -- the "===" operator compares type as well as value
    if ($result === 0) // Success, but no rows were updated
    if ($result > 0) // Success, and updates were done. $result is the number of affected rows.
    

    有关 === 比较运算符的更多信息,请参阅 the PHP manual

    【讨论】:

    • 有没有办法获得更多关于失败原因、错误描述的信息?
    • @Bearwithit 尝试检查$wpdb->last_error。这是一个公共(字符串)变量,应该有最后一个错误的描述。
    • 我会试试的。谢谢!
    • 非常感谢!
    猜你喜欢
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2011-01-18
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多