I need a function for returning an array of services which each contains an id and a name. I've been looking in the mysqli documentation, but cannot verify if there exists smarter solution than this:
function getServices() {
$services = array();
$stmt = $this->db->prepare('SELECT id, name FROM services WHERE client_id=?');
$stmt->bind_param("i", $this->clientId);
$stmt->execute();
$stmt->bind_result($id, $name);
while ($stmt->fetch()) {
$service = array();
$service['id'] = $id;
$service['name'] = $name;
$services[] = $service;
}
$stmt->close();
return $services;
}
It seems a bit cumbersome that I have to build the resulting array myself. Is there a way to let mysql build the array using the selected columns?