我的 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 使用它将返回引用而不是数据。