binemmanuel

PHP get all users functions (Fix)

Sep 21st, 2020
813
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getUsers(Object $db): array
  2. {
  3.     // Prepare an SQL Statement.
  4.     // ...
  5.  
  6.     // Bind result values.
  7.     $stmt->bine_result(
  8.         $id,
  9.         $name,
  10.         $email
  11.     );
  12.  
  13.     // Initialize an empty array.
  14.     // This should contain all users.
  15.     $users = [];
  16.  
  17.     // Fetch data.
  18.     while ($stmt->fetch()) {
  19.         $user = [
  20.             'id' => $id,
  21.             'username' => $username,
  22.             'email' => $email
  23.         ];
  24.  
  25.         // Create a multi-dimensional array
  26.         array_push($users, $user);
  27.     }
  28.  
  29.     return $users;
  30. }
  31.  
RAW Paste Data