PHP MySQL Select Data

PHP MySQL Select Data – Step-by-Step Tutorial

Selecting data from MySQL using PHP allows you to read, display, and process stored information. Almost every dynamic website relies on SELECT queries to show data such as users, products, articles, or reports.

This tutorial explains how to fetch data from MySQL using PHP in a simple and structured way.


What Is PHP MySQL Select Data?

PHP MySQL Select Data refers to retrieving records from a MySQL table using the SQL SELECT statement and displaying them with PHP.

Basic SQL syntax:

SELECT * FROM table_name;

PHP executes this query and processes the result row by row.


Prerequisites for PHP MySQL Select Tutorial

Before starting, you should have:

  • PHP installed
  • MySQL database created
  • MySQLi enabled
  • A table with records
  • Basic PHP knowledge

Sample Database Table Used in Examples

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

PHP MySQL Database Connection Code

<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");

if (!$conn) {
    die("Database connection failed");
}
?>

This connection file will be used in all select examples.


Select All Data from MySQL Table Using PHP

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

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

Output:

Rahul
Amit
Neha

Fetch Data Using mysqli_fetch_assoc in PHP

The mysqli_fetch_assoc() function returns data as an associative array.

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

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

Output:

Name: Rahul
Email: rahul@example.com

Name: Amit
Email: amit@gmail.com

Select Single Record from MySQL Using PHP

<?php
$query = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_assoc($result);

echo $data['name'];
?>

Output:

Rahul

Display MySQL Data in HTML Table Using PHP

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

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

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

Output:

A table displaying all user records from the database.


Select Data Using WHERE Clause in PHP MySQL

<?php
$query = "SELECT * FROM users WHERE age > 25";
$result = mysqli_query($conn, $query);

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

Output:

Amit

Select Data Using ORDER BY Clause

<?php
$query = "SELECT * FROM users ORDER BY name ASC";
$result = mysqli_query($conn, $query);

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

Output:

Amit
Neha
Rahul

Limit Records While Selecting Data in PHP MySQL

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

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

Output:

Rahul
Amit

Secure PHP MySQL Select Using Prepared Statement

<?php
$stmt = mysqli_prepare($conn, "SELECT name FROM users WHERE age > ?");
mysqli_stmt_bind_param($stmt, "i", $age);

$age = 20;
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

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

Output:

Rahul
Amit

Best Practices for PHP MySQL Select Queries

  • Use prepared statements for user input
  • Fetch only required columns
  • Handle empty results properly
  • Use LIMIT for large datasets
  • Close database connection

PHP MySQL select data, PHP read data from MySQL, PHP MySQL fetch records, PHP MySQLi select tutorial, PHP display database records, PHP MySQL SELECT example, PHP database tutorial


Related tutorials you should internally link:


Frequently Asked Questions (FAQ)

1. What does SELECT do in PHP MySQL?

It retrieves data from a database table.

2. Which fetch method is best?

mysqli_fetch_assoc() is most readable and popular.

3. Can PHP select data without MySQL?

No, SELECT works only with databases.

4. How do I select only one column?

Specify column names instead of *.

5. Is prepared SELECT more secure?

Yes, it prevents SQL injection.

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more