Web20 University

Selecting All Rows in MySQL with PHP

Last edited on
Get up to 65% Off Hosting with FREE SSL & FREE Domains!

* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.

When working with databases in PHP, one of the most common tasks is retrieving data from a MySQL database.

Sometimes you might want to select one row with PHP and MySQL, but also understanding how to select all rows from a table is fundamental skill to have too.

In this blog post, we’ll guide you through the process of selecting all rows in a MySQL database using PHP.

Table of Contents

  1. Introduction
  2. Setting Up the Environment
  3. Step-by-Step Process
    • Connect to the MySQL Database
    • Write the SQL Query
    • Execute the Query
    • Fetch and Display the Results
  4. Code Example
  5. Conclusion

1. Introduction

PHP is a popular server-side scripting language often used in combination with MySQL to build dynamic web applications. MySQL is a relational database management system (RDBMS) that allows you to store and retrieve data efficiently. In this post, we will focus on how to fetch all the rows from a MySQL table using PHP.

Whether you’re building a simple blog, an e-commerce site, or any other application that stores data, knowing how to fetch data from a database is crucial. Let’s dive into how you can do this.

2. Setting Up the Environment

Before we begin, ensure that you have the following set up:

  • PHP installed on your system
  • MySQL server installed
  • A MySQL database with at least one table
  • A development environment such as XAMPP or WAMP for local testing

For the purposes of this tutorial, assume that you already have a database named my_database and a table called users with columns id, name, and email.

3. Step-by-Step Process

Step 1: Connect to the MySQL Database

To retrieve data, you first need to establish a connection between your PHP script and the MySQL database.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

In the above code, we are using PHP’s mysqli extension to connect to the database. Replace localhost, root, ``, and my_database with your actual database credentials if they are different.

Step 2: Write the SQL Query

Next, you need to write an SQL query that will fetch all rows from the table. In this case, we want to retrieve all the data from the users table.

$sql = "SELECT * FROM users";

The SELECT * statement retrieves all columns from the specified table (users).

Step 3: Execute the Query

Once you’ve written the query, the next step is to execute it using the mysqli_query() function.

$result = $conn->query($sql);

Step 4: Fetch and Display the Results

If the query runs successfully, the results will be stored in the $result variable. Now, you can loop through the results and display the rows.

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}

This loop will print out the id, name, and email of every user in the users table.

4. Code Example

Here is the complete PHP script that selects all rows from a MySQL table:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

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

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

Example

Here’s an example of what the output might look like in a browser if there are records in your users table:

ID: 1 - Name: John Doe - Email: [email protected]
ID: 2 - Name: Jane Smith - Email: [email protected]

If there are no records in the users table, the output would be:

0 results

5. Conclusion

Selecting all rows from a MySQL database using PHP is a fundamental operation when building dynamic web applications. This tutorial demonstrated how to establish a connection with a MySQL database, execute a query, and display the fetched data in a web browser. Mastering these basic techniques will enable you to work with databases more efficiently as you continue to develop PHP applications.

By understanding how to select data from a database, you’re one step closer to building robust, data-driven web applications. Stay tuned for more tutorials on PHP and MySQL interactions!


Additional Resources:

Get up to 65% Off Hosting with FREE SSL & FREE Domains!