Php 如何在运行时返回此SQL中的所有行
问题已经够广泛了,我还没有在论坛上找到答案,或者至少没有完全找到。由于习惯于使用数组,而且从长远来看并不实用,我决定将自己放在编程对象上 我的问题是:当我运行下面的SQL查询时,我无法恢复所有应该恢复的行。我不知道该怎么做Php 如何在运行时返回此SQL中的所有行,php,mysqli,prepared-statement,Php,Mysqli,Prepared Statement,问题已经够广泛了,我还没有在论坛上找到答案,或者至少没有完全找到。由于习惯于使用数组,而且从长远来看并不实用,我决定将自己放在编程对象上 我的问题是:当我运行下面的SQL查询时,我无法恢复所有应该恢复的行。我不知道该怎么做 public function testing($param) { $return = new stdClass(); if ($request = $this->getConnexion()->prepare('SELECT col1, co
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;
}
}
$return
是您的stdClass对象,因此您只需在每次迭代中覆盖3个属性。使用对象数组:
$return = array();
...
...
while($request->fetch()) {
$item = new stdClass;
$item->col1 = $col1;
$item->col2 = $col2;
$item->col3 = $col3;
$return[] = $item;
}
或者我遗漏了什么?:)<代码>$r=新的StdClass$r->col1=$col1$r->col2=$col2$r->col3=$col3$return[]=$r哦,是的。就这样!大约两个小时我在找这个。我没想到会那样做。非常感谢。