Search code examples
phpmysqlif-statementselectzero

"== 0" Not Working After Select From mysql


I want my code to check if the available copies for a specific book is 0 so that it will update its status to 1(making it unavailable)

Now my problem is that whenever this part of my code runs, the IF statement is always true regardless if the Select statement before it returns a value of 1 or higher. So now, the book becomes unavailable even if it still has copies

$sql = "SELECT Available_Copies from books WHERE id = '$bid'";
$query = $conn->query($sql);
if($sql == 0)
{   
  $sql = $conn->query("UPDATE books SET status = 1 WHERE id = '$bid' ");
  $query = $conn->query($sql);              
}   

Solution

  • Your two queries can be joined into one:

    $sql = "UPDATE books SET status = 1 WHERE id = '$bid' AND Available_Copies = 0";
    $query = $conn->query($sql);