Calling member function in mysqli query
The following method returns an error after being added && in_array($itemID, $userItemIDS)
to the if statement.
Fatal error: Call to a member function bind_param() on a non-object
/**
* Deactivate item.
*
* @param (int) $itemID - Variable representing an item 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;
}
+3
source to share
2 answers
I would isolate the call prepare
and check first to make sure the call succeeds:
$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;
}
It will also make your code easier to read and maintain.
0
source to share
Since $q
will be equal to the boolean result of the operator &&
between
- DB object object (considered positive if success)
- function
in_array
(logical in all cases)
you need to copy the assignment:
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;
}
0
source to share