DEV Community

David Kanekanian
David Kanekanian

Posted on • Edited on

E1 - Creating a PHP page that interacts with the database

Before starting this example, you must have completed the Create a database stage.

Create a file called index.php (see Create project folder) and follow these steps:

  1. Add an HTML html element and a body inside it.

    <html><body></body></html>
    
  2. Add a PHP element to your body.

    <?php ?>
    
  3. Connect to your database as the root user, use a query to get the message from the row just added, then close the connection.

    $databaseLink = new mysqli("localhost", "root", "", "NeatTreats");
    $result = $databaseLink->query("SELECT Message FROM WelcomeMessage WHERE MessageID = 1;");
    
  4. Echo the data so you can see it on the HTML page, then close the database connection.

    echo $result->fetch_assoc()["Message"];
    $databaseLink->close();
    

The final code should look as follows. Test the file in your browser to get a message from the database.

<html><body><?php
$databaseLink = new mysqli("localhost", "root", "", "NeatTreats");
$result = $databaseLink->query("SELECT Message FROM WelcomeMessage WHERE MessageID = 1;");
echo $result->fetch_assoc()["Message"];
$databaseLink->close();
?></body></html>
Enter fullscreen mode Exit fullscreen mode

Parent topic: Example 1

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay