【发布时间】:2019-04-02 14:48:41
【问题描述】:
我目前正在学习面向对象的 PHP,因为我一直使用过程式,我认为是时候了。长话短说,当我尝试调用同一个类中的函数时,我总是收到以下错误“未捕获的错误:调用未定义的函数”,它不起作用。代码如下:
public function getStats( $imgid ) {
$imgid = $this->mysqli->real_escape_string($imgid);
$result = $this->mysqli->query("SELECT * FROM images WHERE imgid = " . $imgid);
if( mysqli_num_rows( $result ) != 1 ) {
print("Image not found!");
exit();
}
$result = $result->fetch_array();
$returnVal = array();
$uploader = getUploaderName($result[1]);
$upvotes = getVotes($imgid, '0');
$downvotes = getVotes($imgid, '1');
array_push($returnVal, $result[0], $uploader, $result[2], $upvotes, $downvotes, $result[3], $result[4]);
return $returnVal;
}
private function getUploaderName( $uid ) {
$result = $this->mysqli->query("SELECT name FROM users WHERE uid = " . $uid);
$result = $result->fetch_array();
return $result[0];
}
private function getVotes( $imgid, $type ) {
# If $type == 0 => Get Upvotes; If $type == 1 => Get Downvotes
$result = $this->mysqli->query("SELECT COUNT(vid) FROM votes_i WHERE imgid = " . $imgid . " AND type = '" . $type . "'");
$result = $result->fetch_array();
return $result[0];
}
这也是在索引中调用函数的部分:
$image = new Image;
if( isset( $_GET['imgid'] ) ) {
$imgStats = $image->getStats( $_GET['imgid'] );
}
我已经尝试将功能从私有更改为公共,但这并没有改变任何事情。
【问题讨论】:
-
$this->getUploaderName($result[1]);等等... -
@AbraCadaver 非常感谢,我太笨了。