1

我正在尝试将数据库结果返回到数组。使用 fetch 时,它只返回数组中的最后一项。

如果我写 $result[$this->topic] .= $a 而不是 $result[$this->topic] = $a,则数组返回所有项目,但仅在一个字符串中。

我的数据库类中的函数,带有while循环:

        public function GetTopic($stmt) {

        if ($stmt->execute() == false) {
            throw new Exception($this->mysqli->error);
        }

        $stmt->bind_result($a); 
        $result = array();

        while ($stmt->fetch()) {
            $result[$this->topic] = $a;
        }

        $stmt->close();

        var_dump($result);
        return $result;
    }

我的处理程序类中的函数,我从数据库类中调用函数:

        public function GetTopics() {

        $query = "SELECT Topic FROM question";
        $stmt = $this->db->Prepare($query);
        $result = $this->db->GetTopic($stmt);

        return $result;
    }

我也尝试过使用 num_rows 和 store_result 而不是 fetch,这也不起作用。任何帮助表示赞赏。

4

1 回答 1

2
$result[$this->topic][] = $a;
于 2012-10-19T16:47:24.683 回答