我不确定这个问题是 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
任何指针表示赞赏。
最佳答案
查看 wp-includes\wp-db.php
。 wpdb 的更新功能的标题注释说:
* @return int|false The number of rows updated, or false on error.
因此,我怀疑您想要找出 false
(表示失败的 bool 值)和 0
(表示没有行的整数)之间的区别返回。)
如果使用==
进行比较,false
和0
是相等的。因此,您需要使用 ===
运算符来检查您处理的是 bool 值 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有关 === 比较运算符的更多信息。
关于php - $wpdb - 它在失败时返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6529242/