我的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
问题是我使用的mysqlli -stmt-fetch将返回一个引用而不是数据。
代码运行良好,因为它应该是到目前为止,我可以看到(除非我错过了什么)。
$result[] = $r;
这一行是向数组中添加新行,而不是重写旧值。老实说,我会质疑其他服务器为您返回的内容。