Dennis Castelijns Dennis Castelijns - 1 month ago 7
PHP Question

Store stmt results in an array, inside of an array

So I am trying to create multiple "result" arrays and fill them with the results of my statement, inside a "main" array.

However it seems to be that the result arrays are getting overwritten each time the loop passes instead of creating an additional array.

My code:

function retrieve_clients() {
$coach_id = $_SESSION['user_details']['id'];

$connection = connection();
$statement = select($connection, '*', 'relations', 'coach_id', '=');
$statement->bind_param('i', $coach_id);

if ($statement->execute()) {
$statement->bind_result($id, $coach_id, $client_id, $relation_date);

while ($statement->fetch()) {
$_SESSION['clients'] = array(
array($id, $coach_id, $client_id, $relation_date)
);
}
}
$statement->close();
}


I can't figure it out anymore. Please spare me because I'm still new to programming.

Answer

You need to add another dimension to your clients, by adding [] to the end of the variable.

$_SESSION['clients'][] = array(
            array($id, $coach_id, $client_id, $relation_date)
        );
Comments