Advertisement
andybeak

Find users with a status

Feb 26th, 2025 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. function getUsersByStatus($desiredStatus) {
  2.     $matchingUsers = [];
  3.    
  4.     $conn = new mysqli("localhost", "db_user", "db_password", "users_database");
  5.    
  6.     if ($conn->connect_error) {
  7.         die("Connection failed: " . $conn->connect_error);
  8.     }
  9.    
  10.     $result = $conn->query("SELECT * FROM users");
  11.    
  12.     while ($row = $result->fetch_assoc()) {
  13.         if ($row['status'] == $desiredStatus) {
  14.             $matchingUsers[] = $row;
  15.         }
  16.     }
  17.    
  18.     $conn->close();
  19.    
  20.     return $matchingUsers;
  21. }
  22.  
  23. // example of using it
  24. $registeredUsers = getUsersByStatus("registered");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement