【问题标题】:how to fix easily this error Fatal error: Call to a member function execute() on boolean in /Applications/XAMPP/xamppfiles/htdocs如何轻松修复此错误致命错误:调用 /Applications/XAMPP/xamppfiles/htdocs 中布尔值的成员函数 execute()
【发布时间】:2018-04-05 07:25:38
【问题描述】:

我开发了php简单页面来注册用户并检查用户是否存在但它不工作并显示休闲错误:

致命错误:第 31 行 /Applications/XAMPP/xamppfiles/htdocs/one/include/DbOperation.php 中的布尔值调用成员函数 execute()

这里的php代码请帮助我们解决这个问题

<?php
class DbOperation
{
    private $conn;


enter code here
    //Constructor
    function __construct()
    {

        require_once('Constants.php');
        require_once('DbConnect.php');
        // opening db connection
        $db = new DbConnect();
        $this->conn = $db->connect();
    }

    //Function to create a new user
    public function createUser($username, $pass, $email, $name, $phone)
    {
        if (!$this->isUserExist($username, $email, $phone)) {
            $password = md5($pass);
            $stmt = $this->conn->prepare("INSERT INTO users (username, password, email, name, phone) VALUES (?, ?, ?, ?, ?)");
            $stmt->bind_param("sssss", $username, $password, $email, $name, $phone);
            if ($stmt->execute()) {
                return USER_CREATED;
            } else {
                return USER_NOT_CREATED;
            }
        } else {
            return USER_ALREADY_EXIST;
        }
    }


    private function isUserExist($username, $email, $phone)
    {

        $stmt = $this->conn->prepare("SELECT id FROM users WHERE username = ? OR email = ? OR phone = ?");
        //if($query = $this->db->conn->prepare($sql)){
        $stmt->bind_param(array("sss", $username, $email, $phone));
        $stmt->execute();
        $stmt->store_result();
        $stmt->fetch();
        $stmt->close();
      return $stmt->num_rows > 0;
  }
}
?>

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    在您的 isUserExist() 函数中,您的 bind_param 似乎有一个不应该存在的数组:

    $stmt-&gt;bind_param(array("sss", $username, $email, $phone));

    应该是:

    $stmt-&gt;bind_param("sss", $username, $email, $phone);

    这很可能是mysqli-&gt;bind_param 返回FALSE 的原因

    【讨论】:

      【解决方案2】:

      如下更改您的isUserExist

      private function isUserExist($username, $email, $phone)
          {
      
              $stmt = $this->conn->prepare("SELECT id FROM users WHERE username = ? OR email = ? OR phone = ?");
              //if($query = $this->db->conn->prepare($sql)){
              $stmt->bind_param("sss", $username, $email, $phone); // change here remove array
              $stmt->execute();
              $stmt->store_result();
              $stmt->fetch();
              //$stmt->close(); // change this comment or remove this
            return $stmt->num_rows > 0;
        }
      

      【讨论】:

        【解决方案3】:

        isUserExist() 函数中使用它

        $stmt->bind_param("sss", $username, $email, $phone);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-04
          • 1970-01-01
          • 2014-09-30
          • 1970-01-01
          • 1970-01-01
          • 2016-10-14
          • 2017-06-07
          • 2018-05-06
          相关资源
          最近更新 更多