致命错误:在第66行的C:\ xampp \ htdocs \ oop_project \ oop_class.php中的非对象上调用成员函数bind_param()

时间:2016-07-27 14:32:18

标签: php prepared-statement

//function to add bm
function add_bm($new_url, $email)
{
   $db = $this->dbm;


  $this->new_url = $new_url;
  $this->email = $email;

   $sql = "select * from bookmark where email='$this->valid_user' and bm_URL='$this->new_url'";

    if(!$stmt = $db->conn->query($sql))
        {

          echo "query failed: (" . $db->conn->errno . ") " .$db->conn->error;
        }else{
       //echo "can check";
       //return true;


    //row count
     if($stmt->num_rows > 0){
       echo "<b><br>Sorry ! <br> The URL had already been added . </b> ";
       return false;
      }else{
     //return true;

     // prepare and bind

        $stmt = $db->conn->prepare("INSERT INTO bookmark (email, bm_URL,) VALUES (?,?)");
        $stmt->bind_param("ss", $this->email, $this->new_url);


// set parameters and execute
    if($stmt->execute()){

            $stmt->close();
        $db->conn->close();

        return true;
            }
        }
        }

}

1 个答案:

答案 0 :(得分:0)

执行数据库操作时,请在继续之前检查错误。否则你会得到意想不到的行为和令人费解的错误,比如这个。

$stmt->bind_param$stmt===false而无效,因为前一条指令有误。

if(!$stmt = $db->conn->prepare("INSERT INTO ...")){
    //something went wrong. This shows the error but handle as appropriate
    die($db->$conn->error);
}
//safe to continue
$stmt->bind_param(...)