public function checkIfItemLeft($itemNameSecond){
$query = "SELECT itemLeft
FROM items
WHERE itemName = '$itemNameSecond'
AND itemLeft > 0 Limit 1";
if(mysqli_query($this->db->getDb(), $query)) {
return true;
}
mysqli_close($this->db->getDb());
return false;
}
How do I check if itemLeft
is greater than 0?
The query could also be done like
$query="SELECT CASE WHEN itemLeft>0 THEN 1 ELSE 0 END
FROM items
WHERE itemName = '$itemNameSecond'";
$result=mysqli_query($this->db->getDb(), $query);
if ($result) { // the condition is necessary in case not record exists ...
$row = mysqli_fetch_row($result);
$largerthan0 = $row[0]; // will contain 1 if the value was >0
}
else {
$largerthan0 = 0;
}
Just thought of a shorter alternative:
public function checkIfItemLeft($itemNameSecond){
$query="SELECT COUNT(*) FROM items
WHERE itemName = '$itemNameSecond' AND itemLeft>0";
$result=mysqli_query($this->db->getDb(), $query);
$row = mysqli_fetch_row($result);
return ($row[0]>0);
}