问题足够广泛,我还没有在论坛上找到答案,或者至少不完全。由于习惯于使用数组,并且从长远来看并没有发现它是实用的,所以我决定将自己置于编程对象中。
这是我的问题:当我运行下面的SQL查询时,我无法恢复我应该恢复的所有行。我不知道该怎么做......
public function testing($param) {
$return = new stdClass();
if ($request = $this->getConnexion()->prepare('SELECT col1, col2, col3 FROM table WHERE col1=?') or die(mysqli_error($this->getConnexion()))) {
$request->bind_param('s', $param);
$request->execute();
$request->bind_result($col1, $col2, $col3);
$request->store_result();
while($request->fetch()) {
$return->col1 = $col1;
$return->col2 = $col2;
$return->col3 = $col3;
/*
Here's what I used to do before
$return[] = array(
'col1' => $col1,
'col2' => $col2,
'col3' => $col3
);
*/
}
$request-> close();
// Here : how to make it understand that I want all the lines and not a single
return $return;
} else {
return false;
}
}
答案 0 :(得分:1)
好$return
是你的stdClass对象,因此你只需要在每次迭代时覆盖3个属性。使用对象数组:
$return = array();
...
...
while($request->fetch()) {
$item = new stdClass;
$item->col1 = $col1;
$item->col2 = $col2;
$item->col3 = $col3;
$return[] = $item;
}