简体   繁体   English

PHP-调用成员函数fetch_assoc()

[英]Php - Call to a member function fetch_assoc()

First I was have: 首先我有:

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

and this function have a problem with get_result() so instead get_result now I write function with BIND and FETCH: 而且此函数的get_result()有问题,所以现在我改用BIND和FETCH编写get_result函数:

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

But on other file index.php now I get: <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 /> 但是现在在其他文件index.php上,我得到: <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 />

so there is a code: 所以有一个代码:

$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 ($task = $result->fetch_assoc()) {    <--HERE IS LINE 155 and ERROR
                $tmp = array();
                $tmp["id"] = $task["id"];
                $tmp["task"] = $task["task"];
                $tmp["status"] = $task["status"];
                $tmp["createdAt"] = $task["created_at"];
                array_push($response["tasks"], $tmp);
            }

            echoRespnse(200, $response);
        });

I try to solve this all day and dont work... Can somebody tell me what is exactly the problem here now? 我整天试图解决这个问题,不工作...有人可以告诉我现在到底是什么问题吗?

What is the problem with this code? 此代码有什么问题?

Something like this (untested code): 这样的东西(未经测试的代码):

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->execute(array($user_id));

    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

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

    $response["error"] = false;
    $response["tasks"] = $db->getAllUserTasks($user_id);

        echoRespnse(200, $response);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM