gpt4 book ai didi

php - `print_r($mysqli,1)` 更改 `$mysqli->affected_rows `

转载 作者:行者123 更新时间:2023-12-02 19:27:49 24 4
gpt4 key购买 nike

我正在使用用户断言函数,例如:

debug_assert (
gettype($ob)=='object',
"Not an object <pre>"
.print_r($ob,1).'</pre>'
) or exit;

但我发现 print_r 在调用 $mysqli 时更改了 $mysqli->affected_rows 的结果:它将受影响的行从之前的 'n' 重置为 -1。

测试代码:

$q= "INSERT INTO t_envois SET id_contact=243";

if (!$mysqli) die ("missing mysqli");
$ok = $mysqli->query($q);
if (!$ok) die ("bad query $q : ".$mysqli->errno.") ".$mysqli->error);

function get_affected_rows() {
global $mysqli;
return $mysqli->affected_rows;
}

echo "1) ".($mysqli->affected_rows)."<br>"; // 1
echo "2) ".($mysqli->affected_rows)."<br>"; // 1
echo "3) ".get_affected_rows()."<br>"; // 1 try other function
echo "4) ".get_affected_rows()."<br>"; // 1 (no issue)
echo "5) ".(print_r($mysqli,1))."<br>"; // affected_rows shown as 1
echo "6) ".($mysqli->affected_rows)."<br>"; // -1 CHANGED !!
echo "7) ".get_affected_rows()."<br>"; // -1 etc

调用 print_r 时结果如何从 1 变为 -1?是否有其他非 sql 函数可以更改 $mysqli 字段?有办法避免这种情况吗?

最佳答案

正如 @Progman 所说,它与一个长期存在的 php bug 有关:http://bugs.php.net/bug.php?id=67348

stat、print_r、var_dump 以及可能的其他调用“stat”的函数会将受影响的行重置为 -1。

现已在 PHP>7.4 中修复,不再有 stat 属性,而是有 stat() 方法

就我而言,php<=7.3 的解决方法是重写一个固定的类似 print_r 的函数:

function print_it($thing) {
if ((gettype ($thing) == 'object')
and (get_class($thing) == 'mysqli'))
echo "...\naffected_rows => ".$thing->affected_rows."...";
else
print_r($thing);
}

关于php - `print_r($mysqli,1)` 更改 `$mysqli->affected_rows `,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58926087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com