Getting the Result as an Array from an SQL Query in PHP

When reading row by row, you can choose not to output each employee immediately but to store them in an array:

php<?php
  $row1 = mysqli_fetch_assoc($res);
  $data[] = $row1;
  
  $row2 = mysqli_fetch_assoc($res);
  $data[] = $row2;
  
  $row3 = mysqli_fetch_assoc($res);
  $data[] = $row3;
  
  $row4 = mysqli_fetch_assoc($res);
  $data[] = $row4;
  
  $row5 = mysqli_fetch_assoc($res);
  $data[] = $row5;
  
  $row6 = mysqli_fetch_assoc($res);
  $data[] = $row6;
?>

As a result, the variable $data will contain the following two-dimensional array:

php<?php
  [
    ['id' => 1, 'name' => 'user1', 'age' => 23, 'salary' => 400],
    ['id' => 2, 'name' => 'user2', 'age' => 25, 'salary' => 500],
    ['id' => 3, 'name' => 'user3', 'age' => 23, 'salary' => 500],
    ['id' => 4, 'name' => 'user4', 'age' => 30, 'salary' => 900],
    ['id' => 5, 'name' => 'user5', 'age' => 27, 'salary' => 500],
    ['id' => 6, 'name' => 'user6', 'age' => 28, 'salary' => 900],
  ]
?>