How about a function like this?
PHP Code:
public function takeItem($item, $qty){
$mysidia = Registry::get("mysidia");
$owned = $mysidia->db->select("inventory", array("quantity"), "itemname ='{$item}' and owner ='{$mysidia->user->username}'")->fetchColumn();
if ($owned >= $qty){
// If the user owns $qty amount or more...
if ($owned == $qty){
// If the user has exactly $qty left, delete the whole row.
$mysidia->db->delete("inventory", "itemname='{$item}' and owner='{$mysidia->user->username}'");
} else {
// Subtract $qty from user's inventory.
$owned_left = $owned - $qty;
$mysidia->db->update("inventory", array("quantity" => $owned_left), "itemname ='{$item}' and owner='{$mysidia->user->username}'");
}
} else {
throw new Exception("Sorry, you only have {$owned} {$item}, you need {$qty}!");
}
}