How can PHP be used to extract information from a MySQL database for inventory purposes?

To extract information from a MySQL database for inventory purposes using PHP, you can establish a connection to the database, query the necessary data, and then process the results to display or use them as needed.

<?php
// Establish a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "inventory";

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

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

// Query the database for inventory information
$sql = "SELECT * FROM products";
$result = $conn->query($sql);

// Process the results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Product Name: " . $row["product_name"]. " - Quantity: " . $row["quantity"]. "<br>";
    }
} else {
    echo "No products found in inventory.";
}

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