dongshen6060 2014-12-01 15:40
浏览 50

Php - 调用成员函数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:

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 />

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?

  • 写回答

1条回答 默认 最新

  • douchangmian0305 2014-12-01 15:51
    关注

    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);
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?