`print_r($ mysqli,1)`改变了$ mysqli-> affected_rows` [英] `print_r($mysqli,1)` changes `$mysqli->affected_rows `

查看:55
本文介绍了`print_r($ mysqli,1)`改变了$ mysqli-> affected_rows`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用用户断言功能,例如:

I was using user-assert functions like :

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

但是我发现在$ mysqli上调用print_r会改变$ mysqli-> affected_rows的结果:它将受影响的行从先前的'n'重置为-1.

but i found out that print_r changes results of $mysqli->affected_rows 's when called on $mysqli : it resets affected_rows from previous 'n' to -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?是否还有其他更改$ mysqli字段的非sql函数?有办法避免这种情况吗?

How can the result change from 1 to -1 when print_r is called ? Are there other non-sql functions that change $mysqli fields ? Is there a way to avoid this ?

推荐答案

正如@Progman所说,这与一个长期存在的php bug有关:

As @Progman stated it's related to a long standing php bug : http://bugs.php.net/bug.php?id=67348

stat,print_r,var_dump以及可能调用"stat"的其他函数会将受影响的行重置为-1.

stat, print_r, var_dump and possibly other function that call 'stat' do reset affected_rows to -1.

就我而言,解决方法是重写固定的类似于print_r的函数:

In my case the workaround has been to rewrite a fixed print_r-like function :

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

这篇关于`print_r($ mysqli,1)`改变了$ mysqli-&gt; affected_rows`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆