php - 如何验证 MySQL 副本?

标签 php mysql

我已经成功地将多个字段从表 foo 复制到表 bar。现在,在我删除原始记录之前,我想验证它是否已全部正确复制。

我尝试使用两个相同的 SQL 查询,将它们分配给变量,然后使用比较运算符比较变量(尝试了 == 和 ===),如下所示:

$sqlFoo = "SELECT `value` FROM `foo` WHERE `user` = '000';";
$sqlBar = "SELECT `value` FROM `bar` WHERE `user` = '000';";

$resultFoo = $db->query($sqlFoo);
$resultBar = $db->query($sqlBar);

If ($resultFoo == $resultBar) {
// Proceed
}

当这不起作用时,我对我的变量使用了 var_dump,它们都返回了:

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(16) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(16) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }

显然我的方法行不通。关于如何完成我想做的事情有什么建议吗?

最佳答案

您不能使用 === 运算符。

根据manual :

When using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.

因此在您的情况下会返回 false,因为它们引用不同的实例。

但是,您可以使用 == 比较它们,这会为您提供所需的结果:

When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.

所以在你的情况下:

if ($sqlFoo_result_object === $sqlBar_result_object) {
  // Proceed
}

关于 == 运算符在您的情况下不起作用:您正在比较查询结果对象而不是文字字符串,对吗?如果你是并且结果仍然是假的,你可能需要获取一行并比较该行的结果而不是 mysqli 结果对象。

关于php - 如何验证 MySQL 副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30416386/

相关文章:

PHP4:通过 cURL 通过 HTTPS/POST 发送 XML?

PHP MySQL 循环遍历?我是 "overlooped"

php - 从Mysql ID查询内容

php - 在 SQL 的插入语句中我有错误?

php - Zend 框架架构

php - Zend Framework 2 中的 SUBSTRING()/MID() 语句

mysql - 查询在 MySQL Workbench 上工作,但在 node.js 中是返回语法问题

mysql - 检查多个列是否存在串联匹配 MySQL

mysql - 选择timestampdiff超过1天并且有一些null的地方

mysql - 如何使用 MySQL Workbench 查找字符串中多次出现的子字符串?