我有一个类 Club{},它有几个公共函数和私有函数。我拥有的三个私有函数用于从我的数据库中获取数据。这是以下代码:
private function Get_Members_From_DB()
{
$sql = "SELECT
Email, FirstName, LastName, Gender
FROM
member";
$result = mysqli_query($this->Con, $sql);
$arrayResult = array();
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
return ($arrayResult);
}
private function Get_Members_Interests_From_DB($MemberEmail)
{
$sql = "SELECT
interest_type.InterestDescription
FROM
member, member_interests, interest_type
WHERE
member.Email = '$MemberEmail'
AND
member.Email = member_interests.Email
AND
member_interests.InterestID = interest_type.InterestID";
$result = mysqli_query($this->Con, $sql);
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
return ($arrayResult);
}
private function Get_Interests_Types_From_DB()
{
$sql = "SELECT
InterestID,
InterestDescription
FROM
interest_type";
$result = mysqli_query($this->Con, $sql);
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
return ($arrayResult);
}
当我调用这些时,我mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
在第 406 行收到警告:即while($row = mysqli_fetch_array($result))
. 我也Fatal error: Cannot use object of type Club as array in
上了第 48 行echo("<tr><td>" . $row1['FirstName'] . " " . $row1['LastName'] . "</td><td>" . $row1['Email'] .
我已经尝试了几个版本,但似乎无法将数据用于以下功能:
public function DisplayMembers()
{
//$members[] = array();
$interests[] = array();
$members = new Club();
$members->Get_Members_From_DB();
echo ("<table id='membertable'><tr><td colspan='4'>Informatics Club Members</td></tr>
<tr><td width='130px'>Name</td><td width='170px'>Email</td><td width='60'>Gender</td>
<td width='280px'>Interests</td></tr>");
while($row1 = $members)
{
echo("<tr><td>" . $row1['FirstName'] . " " . $row1['LastName'] . "</td><td>" . $row1['Email'] .
"</td><td>" . $row1['Gender'] . "</td><td><ul>;");
$results2 = new Club;
$results2->Get_Members_Interests_From_DB($row1['Email']);
while($row2 = mysqli_fetch_array($results2))
{
$interests[] = $row2;
echo("<li>" . $row2['InterestDescription'] . "</li>");
}
echo("</ul></td></tr>");
};
echo "</table><br>";
}
我已更改private function Get_Members_From_DB()
为:
$sql = "SELECT
Email, FirstName, LastName, Gender
FROM
member";
$result = mysqli_query($this->Con, $sql); return ($result);
如果我在上面发布的第一个错误,它仍然无法在DisplayMembers()
函数中工作,除了它会在第 46 行,它将更改为:while($row1 = mysqli_fetch_array($members))
如果我将上面的代码直接粘贴到DisplayMembers()
我可以得到函数正确显示为 HTML 表格。我不明白为什么将代码分成私有函数时会出错。有任何想法吗?
编辑:我从 SAME 类中的公共函数调用这些私有函数。