【问题标题】:Php - add to Array fails, reference addedPhp - 添加到数组失败,添加参考
【发布时间】:2013-06-13 22:17:40
【问题描述】:

我的 php 环境有一些奇怪的问题:

代码如下:

public static function getAllVotesOfGroup($groupid){
    $con = new \SYSTEM\DB\Connection(new \DBD\uVote());
    $res = $con->prepare(   'selVoteByGrp',
                            'SELECT * FROM `uvote_votes` WHERE `group` = ?;',
                            array($groupid));
    $result = array();

    //$r = array();
    while($r = $res->next()){                        
        //print_r($r);                        
        $result[] = $r;
        print_r($result);            
        echo "</br></br>";
    }           
    //print_r($result);
    return $result;
}


public function next($object = false, $result_type = MYSQL_BOTH){        
    if($object){
        $this->current = mysqli_fetch_object($this->res);
    } else {
        $this->current = mysqli_fetch_assoc($this->res);
    }
    return $this->current;
}

在我的另一台机器上,此代码返回我希望这台机器返回的所有值:-> getAllVotesOfGroup(1) 的回显代码

Array ( [0] => Array ( [ID] => 1 [group] => 1 [title] => Test [text] => Testabstimmung [time_start] => 2013-06-12 [time_end] => 2015-06-13 ) ) 

Array ( [0] => Array ( [ID] => 2 [group] => 1 [title] => Test2 [text] => Testabstimmung [time_start] => 2014-06-13 [time_end] => 2012-06-16 ) [1] => Array ( [ID] => 2 [group] => 1 [title] => Test2 [text] => Testabstimmung [time_start] => 2014-06-13 [time_end] => 2012-06-16 ) ) 

Array ( [0] => Array ( [ID] => 3 [group] => 1 [title] => Test3 [text] => bla [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [1] => Array ( [ID] => 3 [group] => 1 [title] => Test3 [text] => bla [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [2] => Array ( [ID] => 3 [group] => 1 [title] => Test3 [text] => bla [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) ) 

Array ( [0] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [1] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [2] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) [3] => Array ( [ID] => 4 [group] => 1 [title] => Test4 [text] => blub [time_start] => 0000-00-00 [time_end] => 0000-00-00 ) ) 

如您所见,之前的值已被替换,可能是因为 $r 是一个引用,并且只有引用被添加到数组中,而不是实际值。

这是为什么呢?我可以设置一些 php.ini 选项来改变这种行为吗?

补充: 这很好用!但这不是我想要的 ;-)

    $result = array();

    while($r = $res->next()){                        
        $result[] = array('title' => $r['title'],'text' => $r['text']);
    }           
    return $result;

好的,我可以解决它;-)

http://www.php.net/manual/en/mysqli-stmt.fetch.php#83284

问题是 mysqli-stmt-fetch im 使用它将返回引用而不是数据。

【问题讨论】:

    标签: php arrays reference add


    【解决方案1】:

    就我所见,代码运行良好(除非我遗漏了什么)。

    $result[] = $r;

    这一行将新行添加到数组中,而不是覆盖旧值。老实说,我会质疑其他服务器为您返回的内容。

    【讨论】:

    • 对不起,彼得,但你没有明白我所说的,问题是/是数组一次又一次地填充了相同的值。这是因为 $res->next() 由于 mysqli-prepare 而返回一个引用。无论如何谢谢;-)
    • 啊抱歉你说得对,我错过了。两台机器上分别使用哪个版本的php
    • 两者都是 php 5.3.3 -> 现在我不确定这实际上是一个配置问题,而不是来自 mysql->mysqli 的移植错误;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    相关资源
    最近更新 更多