How can different database table information be displayed using printf() in PHP?

To display different database table information using printf() in PHP, you can fetch the data from the database using a query, store it in variables, and then use printf() to format and display the information. You can use placeholders in the printf() function to display the data in a structured way.

<?php
// Connect to the 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);
}

// Fetch data from the database
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Display the data using printf()
while($row = $result->fetch_assoc()) {
    printf("ID: %s, Name: %s, Age: %s <br>", $row["id"], $row["name"], $row["age"]);
}

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