Retrieve form data from a MySQL database

This page contains example code to display data that has been previously saved into a MySQL database. For further modification you may need a developer. Add the code below to one of your pages and adjust it to suit. Note that it will not work if added to an HTML file (e.g. page.html), it should be added to a PHP file (e.g. page.php). Follow the instructions below the code to get it working on your server.

1
2
34
5
67
8
9
10
11
12
1314
15
16
17
18
19
202122232425262728293031
32
33
34
35
36
<?php
    /* Step 1: set connection details */
    $db = new mysqli('localhost', 'username', 'password', 'database'); 
    /* Step 2: set the database table */
    $table = 'table'; 
    if ($db->connect_errno) {
        die('Unable to connect to database: ' . $db->connect_error);
    }
 
    /* Step 3: configure the query */
    $query = "SELECT * FROM `$table`;"; 
    $result = $db->query($query);
 
    if (!$result) {
        die('Query error [' . $db->errno . ']: ' . $db->error . "\n\n" . $query);
    } else if ($result->num_rows > 0) {
        echo '<table>';         while ($row = $result->fetch_assoc()) {            echo '<tr>';            echo '<td>' . htmlspecialchars($row['id']) . '</td>';            echo '<td>' . htmlspecialchars($row['name']) . '</td>';            echo '<td>' . htmlspecialchars($row['email']) . '</td>';            echo '</tr>';        }         echo '</table>';    } else {
        echo '0 results';
    }
 
    $db->close();
?>
<?php
    /* Step 1: set connection details */
    $db = new mysqli('localhost', 'username', 'password', 'database');

    /* Step 2: set the database table */
    $table = 'table';

    if ($db->connect_errno) {
        die('Unable to connect to database: ' . $db->connect_error);
    }

    /* Step 3: configure the query */
    $query = "SELECT * FROM `$table`;";

    $result = $db->query($query);

    if (!$result) {
        die('Query error [' . $db->errno . ']: ' . $db->error . "\n\n" . $query);
    } else if ($result->num_rows > 0) {
        echo '<table>';

        while ($row = $result->fetch_assoc()) {
            echo '<tr>';
            echo '<td>' . htmlspecialchars($row['id']) . '</td>';
            echo '<td>' . htmlspecialchars($row['name']) . '</td>';
            echo '<td>' . htmlspecialchars($row['email']) . '</td>';
            echo '</tr>';
        }

        echo '</table>';
    } else {
        echo '0 results';
    }

    $db->close();
?>
  • On line 3, replace username, password and database with the database username, password and database name respectively.
  • On line 6, replace table with the name of the database table.
  • On line 13, you can change the query if necessary. The current code will retrieve all rows from the table.
  • On lines 20-30, you can change the way the results are displayed. The current code will display the results in an HTML table.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy