Dennis Castelijns Dennis Castelijns - 1 year ago 65
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)

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

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download