PHP Code:
class UserManager
{
protected $conn;
public function __construct()
{
$reg = Registry::getInstance();
$this->conn = $reg->getDBConnection();
}
public function getUserById($id)
{
$stmt = $this->conn->prepareStatement('SELECT user_id, user_name, email FROM users WHERE user_id = ?');
$stmt->setInt(1, $id);
$rs = $stmt->executeQuery();
$rs->next();
$user = new User();
$user->setUsername($rs->getString('user_name'));
$user->setUserId($rs->getInt('user_id'));
return $user;
}
}
Note that this is php5 code, with Creole as the db-class. This will of course not work with php4
But should give you an idea. Usage woulde be sth. like
PHP Code:
$um = new Usermanager();
$user = $um->getUserById(32);
You could also add a function setProperties($array) to your user/trainer class, which takes the array from the query and assigns the properties (if the keys and properties have the same name.
hth
Bookmarks