What are the main differences between HTML and PHP in web development?
HTML is a markup language used for creating the structure of a webpage, while PHP is a server-side scripting language used for creating dynamic web pages. HTML is static and only defines the layout and content of a webpage, while PHP allows for embedding dynamic content, executing server-side scripts, and interacting with databases. PHP code is executed on the server before the HTML is sent to the client's browser, allowing for more complex and interactive web applications.
<?php
// PHP code to connect to a database and retrieve dynamic content
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to retrieve data from a table
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- What are the common pitfalls to avoid when working with dates and timestamps in PHP and MySQL to ensure accurate data insertion and retrieval?
- What are the advantages of using INSERT queries with multiple values compared to individual UPDATE queries in mysqli?
- How can you create a survey in PHP that sends results via email or stores them in a database?