Pogledajte određenu poruku
Staro 01. 12. 2014.   #1
mslavko
profesionalac
Professional
 
Avatar mslavko
 
Datum učlanjenja: 25.10.2012
Poruke: 278
Hvala: 16
32 "Hvala" u 9 poruka
mslavko is on a distinguished road
Default Get_result ne radi - zamena FETCH i BIND

Kreiram REST api na backend strani najpre sam imao funciju:

Kd:
public function getAllUserTasks($user_id) {
        $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $stmt->bind_result();
        $tasks = $stmt->get_result();
        $stmt->close();
        return $tasks;
    }
funkcija get_result ne radi pa sam moram da zamenim sa FETCH i BIND kako predlazu na netu...

onda sam napisao:

Kd:
public function getAllUserTasks($user_id) {
        $stmt = $this->conn->prepare("SELECT t.* FROM tasks t, user_tasks ut WHERE t.id = ut.task_id AND ut.user_id = ?");
        $stmt->bind_param("i", $user_id);
        $stmt->execute();
        $tasks = array();
            $stmt->bind_result($id, $task, $status, $created_at);

            $stmt->fetch();
            $tasks["id"] = $id;
            $tasks["task"] = $task;
            $tasks["status"] = $status;
            $tasks["created_at"] = $created_at;
            $stmt->close();
            return $tasks;
    }
i na index.php:

Kd:
$app->get('/tasks', 'authenticate', function() {
            global $user_id;
            $response = array();
            $db = new DbHandler();

            // fetching all user tasks
            $result = $db->getAllUserTasks($user_id);

            $response["error"] = false;
            $response["tasks"] = array();

            // looping through result and preparing tasks array
            while ($result->fetch_assoc()) {
                //$tmp = array();
                $response["error"] = false;
                $response["id"] = $result["id"];
                $response["task"] = $result["task"];
                $response["status"] = $result["status"];
                $response["createdAt"] = $result["created_at"];
                array_push($response["tasks"], $result);
            }

            echoRespnse(200, $response);
        });
medjutim sada dobijam gresku:
Citat:
<b>Fatal error</b>: Call to a member function fetch_assoc() on a non-object in <b>/home/agroagro/public_html/agroMobile/v1/index.php</b> on line <b>155</b><br />
Kako da resim ovaj problem? Gde gresim?
mslavko je offline   Odgovorite uz citat