【问题标题】:`print_r($mysqli,1)` changes `$mysqli->affected_rows ``print_r($mysqli,1)` 改变 `$mysqli->affected_rows `
【发布时间】:2018-12-17 11:04:16
【问题描述】:

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

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

但我发现 print_r 在 $mysqli 上调用时会更改 $mysqli->affected_rows 的结果:它将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 字段?有没有办法避免这种情况?

【问题讨论】:

  • see 1 1st call 是什么意思?
  • "see 1 1st call" 意味着您可以在 print_r 输出中看到affected_rows 为1。在示例中只有一次调用print_r,所以我编辑了该评论以反映这一点。跨度>
  • 也可以使用var_dump() 在 Linux 上的 PHP 7.2.10 上确认这一点。可能与bugs.php.net/bug.php?id=67348有关。
  • 是的@Progman,它看起来像。所以这是一个错误。

标签: php mysqli


【解决方案1】:

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

stat、print_r、var_dump 和可能的其他调用“stat”的函数会将affected_rows 重置为-1。

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

在我的情况下,php

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

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多