下面的方法在我向if语句添加&& in_array($itemID, $userItemIDS)
之后返回一个错误。Fatal error: Call to a member function bind_param() on a non-object
/**
* Deactivate item.
*
* @param (int) $itemID - Variable representing an item's id.
*/
public function deactivate($itemID) {
//get user item ids
$userItemIDS = $this->helperClass->userItemIDS();
if( $q = $this->db->mysqli->prepare("UPDATE items SET active = 0 WHERE id = ?") && in_array($itemID, $userItemIDS) )
{
$q->bind_param("i", $itemID);
$q->execute();
$q->close();
return true;
}
return false;
}
最佳答案
我会将调用分离到prepare
并首先检查以确保调用成功:
$q = $this->db->mysqli->prepare("UPDATE items SET active = 0 WHERE id = ?");
if ($q != FALSE && in_array($itemID, $userItemIDS)) {
$q->bind_param("i", $itemID);
$q->execute();
$q->close();
return true;
}
这也将使您的代码更易于阅读和维护。