如何 return 此 SQL 中的所有行同时

How to return all the rows in this SQL while

问题太广泛了,我在论坛上没有找到答案,或者至少没有找到答案。习惯用数组,长篇大论觉得不实用运行,决定把自己放在编程对象里

这是我的问题:当我 运行 下面的 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;
    }

}

好吧 $return 是您的 stdClass 对象,因此您只是在每次迭代时覆盖 3 个属性。使用对象数组:

$return = array();

...
...

while($request->fetch()) {

    $item = new stdClass;
    $item->col1 = $col1;
    $item->col2 = $col2;
    $item->col3 = $col3;

    $return[] = $item;
}