dpxf81245 2014-09-08 06:52
浏览 50
已采纳

致命错误:在非对象上调用成员函数bind_param()

I try some codes to build a RESTful API Web Service using PHP and slim framework. (I'm using Advanced Rest Client for testing this code)

This is my codes.

private function isUserExists($email) { $stmt = $this->conn->prepare("SELECT id from users WHERE email = ?"); $stmt->bind_param("s", $email); $stmt->execute(); $stmt->store_result(); $num_rows = $stmt->num_rows; $stmt->close(); return $num_rows > 0; }

and then I got this error.

Fatal Error : Call to a member function bind_param() on a non-object on Line 113

Line 113 is : $stmt->bind_param("s", $email);

I've been looking for a solution of this problem since 2 weeks ago.

Thanks before guys :))

  • 写回答

1条回答 默认 最新

  • dousao1175 2014-09-08 07:00
    关注

    The problem lies in $this->conn->prepare, which does not return an object, as you would expect.

    For mysqli, prepare() returns FALSE on error. You can check the error message using $this->conn->error.

    This code should print your error in case of failure:

    private function isUserExists($email) {
        $stmt = $this->conn->prepare("SELECT id from users WHERE email = ?");
        if ($stmt === FALSE){
            die($this->conn->error);
        } else  {
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $stmt->store_result();
            $num_rows = $stmt->num_rows;
            $stmt->close();
            return $num_rows > 0;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?
  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿