How can PHP be integrated with MySQL for database management?

To integrate PHP with MySQL for database management, you can use the MySQLi or PDO extension in PHP. These extensions allow you to connect to a MySQL database, execute queries, and retrieve data from the database using PHP scripts.

// Create a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

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

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

// Perform a query
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Fetch data from the result set
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

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