How can PHP be integrated with MySQL and HTML to create dynamic web applications?
To integrate PHP with MySQL and HTML to create dynamic web applications, you can use PHP to connect to the MySQL database, retrieve data, and then dynamically generate HTML content to display the data on the web page.
<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve data from MySQL database
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
// Display data in HTML
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<p>" . $row["column1"] . " - " . $row["column2"] . "</p>";
}
} else {
echo "0 results";
}
$conn->close();
?>