How to Fetch Single Row Data Value from Database in PHP MySQL

By combining the database connectivity capabilities of PHP with the tabular representation of HTML, developers can efficiently retrieve/fetch all data from a database and display it in html table a user-friendly way for their PHP projects. But, you may need to retrieve/get a row of data from the MySQL database using PHP and display it in HTML table.

In this tutorial, you will learn how to fetch a single row data value from a database using PHP MySQL and Display it in an HTML table.

How to Fetch Single Row Data Value from Database in PHP MySQL and Display

Steps to retrieve/fetch a single row of data from a MySQL database using PHP and display it in an HTML table.:

  • Step 1: Set up the Database
  • Step 2: Connect to the Database
  • Step 3: Fetch the Single Row Data and Display it in HTML table
  • Step 4: Test the Code

Step 1: Set up the Database

Make sure you have a MySQL database set up with a table containing the data you want to fetch. For this tutorial, we’ll use a table named “users” with the following structure.

So, open your phpmyadmin and execute the following quries in it to create table in database:

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

INSERT INTO users (name, email, age) VALUES
('John Doe', '[email protected]', 30),
('Jane Smith', '[email protected]', 25),
('Bob Johnson', '[email protected]', 35);

Step 2: Connect to the Database

Next, you need to connect database to php project. So, create a new PHP file in your project directory, which name database.php. This file will handle the database connection, data retrieval, and display.

<?php
// Replace with your database credentials
$servername = "localhost";
$username = "your_mysql_username";
$password = "your_mysql_password";
$dbname = "your_database_name";

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

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

Note that :- Replace “hostname,” “username,” “password,” and “database_name” with your actual database credentials.

Step 3: Fetch the Single Row Data and Display it in HTML table

Now, you need to create fetch_single_row.php file to fetch a single row from the “users” table based on a specific condition (e.g., user ID). In this example, fetch the user with ID = 2.

<?php
// Assuming the user ID is passed as a parameter (you can get it from the URL or POST data)

include "database.php";

if (isset($_GET['id'])) {
    // Sanitize the input to prevent SQL injection
    $user_id = intval($_GET['id']);

    // Prepare the SQL query
    $sql = "SELECT * FROM users WHERE id = $user_id LIMIT 1";

    // Execute the query
    $result = $conn->query($sql);

    // Check if a row was found
    if ($result->num_rows == 1) {
        // Fetch the data as an associative array
        $row = $result->fetch_assoc();

        // Extract the values
        $name = $row['name'];
        $email = $row['email'];
        $age = $row['age'];

        // Now you have the data, and you can display it on the web page
    } else {
        echo "No user found with ID: $user_id";
    }
}
?>

// display single row data or record from mysql database using php code
<!DOCTYPE html>
<html>
<head>
    <title>User Details</title>
</head>
<body>
    <?php if (isset($name)): ?>
        <h1>User Details</h1>
        <p><strong>Name:</strong> <?php echo $name; ?></p>
        <p><strong>Email:</strong> <?php echo $email; ?></p>
        <p><strong>Age:</strong> <?php echo $age; ?></p>
    <?php else: ?>
        <p>No user found.</p>
    <?php endif; ?>
</body>
</html>

Step 4: Test the Code

Save the fetch_single_row.php file and run it by visiting the URL in your web browser (e.g., http://localhost/test-project/fetch_single_row.php?id=1).

Make sure to replace “localhost” with your server’s domain name or IP address (e.g test-project).

Conclusion

That’s it, You have successfully learned how to fetch and display single row data value or record from MySQL database in PHP.

Recommended Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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