function saveSettings($userID, $settings){ //save user settings for given userID $db = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password'); $stmt = $db->prepare("UPDATE users SET settings = ? WHERE userID = ?"); $stmt->execute([$settings, $userID]); return true; } saveSettings(1, 'theme=dark¬ifications=enabled');
class User { private $userID; private $settings; function __construct($userID){ //initialize user object with saved settings $db = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password'); $stmt = $db->prepare("SELECT settings FROM users WHERE userID = ?"); $stmt->execute([$userID]); $this->userID = $userID; $this->settings = $stmt->fetchColumn(); } function saveSettings($newSettings){ //save updated settings for current user $this->settings = $newSettings; $db = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password'); $stmt = $db->prepare("UPDATE users SET settings = ? WHERE userID = ?"); $stmt->execute([$newSettings, $this->userID]); return true; } } $user = new User(1); $user->saveSettings('theme=light¬ifications=disabled');In this example, the User class is used to encapsulate the user's information including their settings/preferences. The saveSettings function is a method of the User class and updates the user's settings in the database. Package/library: PDO, a PHP extension that provides a data-access abstraction layer for accessing databases.