Here's a quick snippet for fetching that data you got in the DB.

HTML

Create a Form in HTML


<form action="./getUsers.php" method="get">
    <input type="submit" value="Submit">
</form>
                            
  • Form uses "get" as method
  • The action is the path to the PHP file. We'll create a new PHP file for this action, called getUsers.php.

getUsers.php


<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Page title</title>
    </head>
    <body>
        <?php
        $servername = "localhost";
        $database = "rikundev_testusers";
        $username = "rikundev_testuser";
        $password = "7Cmny=k15vK)";
        $conn = mysqli_connect($servername, $username, $password, $database);
        if (!$conn) {
            die("Connection failed (in get users): " . $conn->connect_error);
        }

        else{
            //SELECT -query
            $sql = "SELECT * FROM users";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    echo "id: " . $row["user_id"]
                    . " - Username: " . $row["fullname"]
                    . " - Email: " . $row["email"]
                    . "<br>";
                }
            } else {
                echo "0 results";
            }
            $conn->close();
        }
        ?>
    </body>
</html>
                        
  • The code is much the same as with the injection PHP.
  • Query has changed, and we use while and fetch_assoc() to iterate through all the rows of the table.
  • Outcome is echoed as plain text on HTML. Next challenge could be to include styles for the data.

All done! Quick recap coming up next!

1

2

3

4