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

时间:2016-08-05 11:59:31

标签: php android mysql

当我运行此代码时,出现以下错误:

Fatal error call to a member function bind_param() on a non-object.

以下是该函数的代码:

public function storeUser($name, $email, $password, $phone, $address1, $address2) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt

    $stmt = $this->conn->prepare("INSERT INTO `users`(`id`, `unique_id`, `name`, `email`, `phone`, `address1`, `address2`, `encrypted_password`, `salt`, `created_at`) VALUES (?,?,?,?,?,?,?,?,NOW())");
    $stmt->bind_param("ssssssss", $uuid, $name, $email, $phone, $address1, $address2, $encrypted_password, $salt);
    $result = $stmt->execute();
    $stmt->close();

    // check for successful store
    if ($result) {
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $user = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        return $user;
    } else {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个pdo示例,因为未指定连接方法

public function connection ($username, $password, $servername, $databasename)
{
    $this->conn = new PDO("mysql:host=$servername;dbname=$databasename", $username, $password);
    return true;
}
public function storeUser($name, $email, $password, $phone, $address1, $address2) 
{ 
    $uuid = uniqid('', true); $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password 
    $salt = $hash["salt"]; // salt 
    $stmt = $this->conn->prepare("INSERT INTO `users`(`id`, `unique_id`, `name`, `email`, `phone`, `address1`, `address2`, `encrypted_password`, `salt`, `created_at`) VALUES (?,?,?,?,?,?,?,?,NOW())"); 
    $stmt->bindValue(1, $uuid, PDO::PARAM_STR);
    $stmt->bindValue (2, $name, PDO::PARAM_STR); 
    $stmt->bindValue (3, $email, PDO::PARAM_STR);
    $stmt->bindValue (4, $phone, PDO::PARAM_STR);
    $stmt->bindValue (5, $address1, PDO::PARAM_STR);
    $stmt->bindValue (6, $address2, PDO::PARAM_STR);
    $stmt->bindValue (7, $encrypted_password, PDO::PARAM_STR); 
    $stmt->bindValue (8, $salt, PDO::PARAM_STR); 
    $result = $stmt->execute();
    if ($result) 
    { 
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?"); 
        $stmt->bind_param(1, $email PDO::PARAM_STR); 
        $stmt->execute(); 
        $user = $stmt->fetch(PDO::FETCH_ASSOC); 
        return $user; 
    }
    else 
    {
         return false; 
    } 
}