0

I'm currently learning Object Oriented PHP as I always used procedural and I thought it's about time. Long story short, I always get the following error "Uncaught Error: Call to undefined function" when I try to call a function that is within the same class, it wont work. Here the code:

  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];

  }

And also here's the part where the function is being called in the index:

$image = new Image;

if( isset( $_GET['imgid'] ) ) {
  $imgStats = $image->getStats( $_GET['imgid'] );
}

I already tried to change the function from private to public, but that doesn't change a thing.

Laurent
  • 39
  • 1
  • 5

1 Answers1

1

Have you tried adding $this-> before your function? I assume its in a class, and I think you need to refer to the object you are working with.

$this->functionName();

This answer goes into more details: https://stackoverflow.com/a/1523484/9186388

Fraser
  • 52
  • 4