Preloader

Fetch Data from Database Using PHP and MySQL

PHP Tutorials
codevigyaan php

How to fetch data from mysql database with PHP and show on webpage. Displays data from saved database using php, so it can show mysql arrays from user, blog, product list or other data.

Fetching data is very easily being one of the essential skill of PHP because nearly every dynamic website requires fetching data from a database.

Why Fetching Data Is Important

Websites are not just repositories of data. They are also thoroughfares of data.

For example, a website may need to:

  • Show registered users
  • Show all blog posts
  • List products
  • Load user profiles

Without fetching data, stored information cannot be used.

Requirements Before Fetching Data

Before retrieving the data, ensure the following are prepared.

  • PHP is linked to MySQL
  • There is existing database
  • A table contains data
  • The connection file works

Once that’s done, fetching the data is fairly simple.

Example Database Table

Here is a basic users table.

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100)
);

In this table are stored also the user names and the email.

SQL Query to Fetch Data

PHP retrieves the data from MySQL using the SELECT query.

SELECT * FROM users;

This query will return all records in the table.

Fetch Data Using PHP (MySQLi)

Now just fetch the data using PHP.

<?php
include "db.php";

$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . " - " . $row['email'] . "<br>";
}
?>

This code reads each row and displays it on the page.

How the Fetch Process Works

The process is just like very simple steps.

First, PHP sends a SELECT query
Next, MySQL returns the data
Then, PHP reads one row at a time
Lastly, the data is displayed on the page

Hence users have access to view stored records.

Fetch Data in an HTML Table

Table presentation of data looks more elegant.

<table border="1">
<tr>
    <th>Name</th>
    <th>Email</th>
</tr>

<?php
include "db.php";

$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</table>

It Can be used for admin panels.

Checking If Data Exists

Always double-check before showing data.

if (mysqli_num_rows($result) > 0) {
    // data exists
} else {
    echo "No records found";
}

This prevents empty output.

Common Beginner Mistakes

These are common mistakes among novice/new users.

  • Wrong name of the table
  • Incorrect column name
  • Forget about database connection
  • Not checking query results

Correcting these problems takes less time.

Best Practices for Fetching Data

Follow these tips to write better code:

  • Always check query results
  • Use valid column names.
  • Display data safely
  • Later, use prepared statements.
  • Keep code clean and readable

They can be used in real projects.

Summary

Fetch data from a database in PHP/mysql to display data that stored in the database on a webside. It is done by using the SELECT requests, PHP loops and the database connection. When he done properly, it can be used to build dashboards, list of users, dynamic pages, and so.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *