gpt4 book ai didi

php - 如何在 PHP 中返回从 MySQL 获取的数组

转载 作者:行者123 更新时间:2023-11-29 21:50:08 25 4
gpt4 key购买 nike

我试图返回一个从存储在 PHP 文件中的公共(public)函数调用的数组。我需要导出的返回数组是 JSON 格式。我有下面的代码用于调用数组(在其他情况下有效),但输出只是数组(phpMyAdmin 中的 sql 返回所有数据)

这是应该返回数组的公共(public)函数,该数组存储在通用 PHP 类文件中。

public function getIssueList() {
$sql = "select * from IssueData";
$returnValue = array();

$result = $this->conn->query($sql); // makes the connection and executes the sql

if ($result != null) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}

return $returnValue;

}

然后我从下面的代码中调用公共(public)函数:

$result = $dao->getIssueList(); //opens the connection and calls the public function

echo $result;

但是我得到的回显结果只是“Array”这个词

上面的代码适用于其他公共(public)函数,但它只返回一行,而不是我在本例中需要的多行。我还需要将数组设为关联数组。

可能出了什么问题?

最佳答案

您可以 json_encode ( http://php.net/manual/en/function.json-encode.php )

echo json_encode($result);

关于php - 如何在 PHP 中返回从 MySQL 获取的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33724810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com