【发布时间】:2015-11-15 20:43:04
【问题描述】:
我试图返回一个从存储在 PHP 文件中的公共函数调用的数组。 我需要导出的返回数组是 JSON 格式。 我有以下代码用于调用数组(在其他情况下有效),但输出只是数组(phpMyAdmin 中的 sql 返回所有数据)
这是应该返回数组的公共函数,存储在一个通用的 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;
}
然后我从下面的代码调用公共函数:
$result = $dao->getIssueList(); //opens the connection and calls the public function
echo $result;
但我得到的回显结果只是“数组”这个词
上面的代码适用于其他公共函数,但在这种情况下它只返回一行而不是多行。 我还需要将数组作为关联数组。
可能出了什么问题?
【问题讨论】:
-
将
$returnValue = $row;更改为$returnValue[] = $row;和echo $result;更改为print_r($result);。 -
(1)
->fecth_array()-> Fetch a 结果行...,所以如果你想要多行,你需要做一个循环,并将每一行添加到大批。 (2) 因为return $returnValue;会返回一个数组,所以你不能只是echo $result;。您可以使用print_r($result);或var_dump($result)或 ...