How to Get First Row of Table in PHP MySQL?

Feb 26, 2022 . Admin

Hello Friends,

I am going to explain to you an example of how to get the first row of the table in PHP?. You will learn to find the first row of the table in the php query. Inside this article, we will see the get first row in select query.

To return only the first row that matches your SELECT query, you need to add the LIMIT clause to your SELECT statement.

I will give you simple query to getting the first row of the table in PHP.

Table: users


So, let's see bellow solution:

index.php
<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db_php";

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

$sql = "SELECT 
            * 
            FROM `users` 
        LIMIT 1";

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

while ($row = $result->fetch_assoc()) {
    echo '<pre>';
    print_r($row);
}

?>
Output:
Array
(
    [id] => 1
    [name] => nikhil thumar
    [email] => nik123@gmail.com
    [created_at] => 2022-01-08 09:28:17
    [updated_at] => 2020-02-01 09:28:17
    [expired_at] => 2022-03-17 09:28:17
)
It will help you...
#PHP