[SOLVED] PHP class functions

Issue

I have problem with functions. I call getclass function with this.

<?php 
require_once './DbOperations.php';

if($_SERVER['REQUEST_METHOD']=='POST'){ 
    if(isset($_POST['username'])) {
        $db = new DbOperations();
        
        $classjson = $db->getclass($_POST['username']);
        echo $classjson;
    }
}
?>

But its not working for me. This is not working [$teacherfullname,$teacherpicture] = $this->getteacher($teacher);
But when i put this top and change $teacher to $uuid its good. But i need there $teacher.

        public function getclass($uuid) {
//when i put here its working with $uuid but i need $teacher
[$teacherfullname,$teacherpicture] = $this->getteacher($uuid);
            $stmt = $this->con->prepare("SELECT `id`, `name`, `teacher`, `student` FROM `class` WHERE teacher= ? OR student= ?");
            $stmt->bind_param("ss", $uuid, $uuid);
            $stmt->execute();
            $stmt->bind_result($id, $name, $teacher, $student);
            
            $product = array();

            while($stmt->fetch()) {

               //this is what not working 
               [$teacherfullname,$teacherpicture] = $this->getteacher($teacher);
                $temp = array();
                $temp['id'] = $id;
                $temp['name'] = $name;
                $temp['teacher'] = $teacherfullname;
                $temp['picture'] = $teacherpicture;
                $temp['student'] = $student;
                array_push($product, $temp);
            }
            return json_encode($product);
        }

        private function getteacher($uuid) {
            $stmt = $this->con->prepare("SELECT id, fullname, picture FROM users WHERE uuid = ?");
            $stmt->bind_param("s", $uuid);
            $stmt->execute();
            $result = $stmt->get_result();
            $row = $result->fetch_assoc();
            $stmt->store_result();
            return [$row['fullname'], $row['picture']];
        }

Help me to find the bug please.

Solution

Edit : misreading from the OP, sorry.

Try to replace

$stmt->bind_param("s", $uuid);

With :

$stmt->bind_param("uuid", $uuid);

Don’t assign variables like this.

[$teacherfullname,$teacherpicture] = $this->getteacher($teacher);

I suggest you to do something like this :

...
$teacher = $this->getteacher($teacher);
$temp['teacher'] = $teacher['fullname'];
$temp['picture'] = $teacher['picture'];
...

private function getteacher($uuid) {
    ... //use named indexes, easier to maintain
    return ["fullname" => $row['fullname'], "picture" => $row['picture']];
}

Answered By – mt.i.1

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.