How to Retrieve or Fetch Data From Database in PHP

How to Retrieve or Fetch Data From Database in PHP

As a developer, you’ll often need to fetch data from a database to display it on your website or application. In this tutorial, we’ll look at how to retrieve data from a MySQL database using PHP.

Here are the Steps on How to Retrieve or Fetch Data From Database in PHP

How to Fetch Data From Database in PHP

Step 1 : Connect to the Database

The first step in retrieving data from a database is to establish a connection. To do this, we use the PHP function mysqli_connect(). This function takes four parameters: the hostname, username, password, and database name.,

So, We are storing mysqli connection details in conn variable, as shown below.

<?php
$conn=mysqli_connect("localhost","username","password","databasename");

if (!$conn)
{
    die("Connection failed: " . mysqli_connect_error());
}
?>

This code establishes a connection to the database and checks if the connection was successful. If the connection fails, it will display an error message.

Step 2: Query the Database

Once we have established a connection to the database, we can query it to retrieve the data we need. We’ll use the mysqli_query() function to send a query to the database.

<?php
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<p>" . $row["firstname"] . " " . $row["lastname"] . "</p>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

In this code, we are selecting all the rows from a table called users. We then use a while loop to iterate through the results and display them. In this example, we are displaying the firstname and lastname columns from each row as shown below…

Fetch Data from Database in php

Step 3: Display the Data in table format

Now that we have retrieved the data from the database, we can display it on our website or application. We’ll use HTML tags to format the data and make it easier to read.

<?php
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    echo "<table>";
    echo "<tr><th>First Name</th><th>Last Name</th></tr>";
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr><td>" . $row["firstname"] . "</td><td>" . $row["lastname"] . "</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

In this code, we are displaying the results in an HTML table. We use the mysqli_fetch_assoc() function to retrieve each row as an associative array, and then use the array values to populate the table cells. In this code, we are displaying the results in an HTML table as shown below…

Conclusion

Retrieving data from a database in PHP is a common task for developers. By following these steps, you can establish a database connection, query the database, and display the results in a format that is easy to read and understand. With these skills, you’ll be able to build dynamic websites and applications that can retrieve and display data on demand.

Also Check : PHP Code for Inserting Data into Database from Form (rahulvijayam.com)

If you have any questions or suggestions, please feel free to leave a comment below.