Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 1.03 KB

how-to-get-the-first-row-of-a-result-in-mysql-using-php.md

File metadata and controls

33 lines (24 loc) · 1.03 KB

How to get the first row of a result in MySQL using PHP?

// plain

To get the first row of a result in MySQL using PHP, you can use the mysqli_fetch_assoc() function. This function takes a result set as an argument and returns an associative array of the first row.

Example code

$result = mysqli_query($conn, "SELECT * FROM table");
$row = mysqli_fetch_assoc($result);

Output example

Array
(
    [column1] => value1
    [column2] => value2
    [column3] => value3
)

Code explanation

  • mysqli_query($conn, "SELECT * FROM table"): This line executes a query on the MySQL database and returns a result set.
  • mysqli_fetch_assoc($result): This line takes the result set as an argument and returns an associative array of the first row.

Helpful links

onelinerhub: How to get the first row of a result in MySQL using PHP?