How can one display an icon and link it to a database in PHP?
To display an icon and link it to a database in PHP, you can use HTML code within your PHP file. You can create an anchor tag `<a>` with the icon image as its content and set the `href` attribute to a PHP file that connects to the database. Inside the PHP file, you can write the code to fetch data from the database and display it accordingly.
<!-- HTML code to display icon linked to database -->
<a href="database.php"><img src="icon.png" alt="Database icon"></a>
<?php
// database.php
// Connect to database and fetch data
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Data: " . $row["column"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- How can the use of absolute and relative DateTime formats in PHP arrays improve date calculations in SQL queries?
- How can PHP be integrated with a database to store and retrieve birthday information efficiently?
- What are the common pitfalls to avoid when using file_get_contents() to download files in PHP?