// Connect to the database $host = 'localhost'; $user = 'root'; $password = ''; $database = 'myapp'; $conn = mysqli_connect($host, $user, $password, $database); // Define function to get user info function getUserInfo($userId, $conn) { // Query the database for user information $query = "SELECT name, email, profile_picture FROM users WHERE id = $userId "; $result = mysqli_query($conn, $query); // Store user info in an array $userInfo = []; while ($row = mysqli_fetch_assoc($result)) { $userInfo = $row; } // Return the user info array return $userInfo; } // Call the function to get user info for user with ID 1 $userInfo = getUserInfo(1, $conn);
// Install the Guzzle HTTP client library via Composer require_once 'vendor/autoload.php'; use GuzzleHttp\Client; // Define function to get user info function getUserInfo($userId) { // Make API request to retrieve user information $client = new Client(['base_uri' => 'https://myapi.com']); $response = $client->request('GET', '/users/' . $userId); // Decode the response JSON into an array $userInfo = json_decode($response->getBody(), true); // Return the user info array return $userInfo; } // Call the function to get user info for user with ID 1 $userInfo = getUserInfo(1);In Example 1, the code is using the MySQLi package to connect to a MySQL database and retrieve user information. In Example 2, the code is using the Guzzle package to make an HTTP request to an API and retrieve user information.