In what scenarios would it be more beneficial to use SQL queries to check for product availability, rather than PHP functions?

In scenarios where the product availability information is stored in a database, it would be more beneficial to use SQL queries to check for product availability rather than PHP functions. SQL queries can efficiently retrieve the necessary information directly from the database, reducing the need for complex PHP logic to process and filter the data.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL query to check product availability
$sql = "SELECT * FROM products WHERE availability = 'available'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Product is available
    echo "Product is available";
} else {
    // Product is not available
    echo "Product is not available";
}

// Close the database connection
$conn->close();