How can you display database content only when a button is clicked in PHP?

To display database content only when a button is clicked in PHP, you can use a combination of HTML form and PHP script. You can create a form with a submit button, and upon clicking the button, the form will submit to a PHP script that retrieves and displays the database content. By using the isset() function in PHP to check if the button has been clicked, you can control when the database content is displayed.

<?php
// Check if the button is clicked
if(isset($_POST['display_button'])) {
    // Connect to the database and retrieve content
    $conn = new mysqli("localhost", "username", "password", "database");
    $result = $conn->query("SELECT * FROM table");

    // Display the content
    while($row = $result->fetch_assoc()) {
        echo $row['column_name'] . "<br>";
    }

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

<form method="post">
    <input type="submit" name="display_button" value="Display Database Content">
</form>