1

I created a script where I set a array in an existing array through a while loop from a SQL database.

while ($nextday = odbc_fetch_array($nextdayinfo)){
$username = $nextday['user_sign'];
  if(isset($username)){
  $nextday[] = array('value' => $username, 'text' => $username);
  }
}

This is the code. If I try to print_r($nextday) after the IF clause, it will show me all the information, as soon as i put the print_r($nextday) after the while clause, it stops working.

4
  • What is $nextday? Array? Or result returned from _fetch? Commented Mar 23, 2017 at 12:52
  • $nextday is an empty array. I want to create it in the while clausule. Commented Mar 23, 2017 at 12:52
  • 1
    But your array has the same name as the database row, so it is overwritten on every iteration. Commented Mar 23, 2017 at 12:52
  • @Jerodev CCCCCCCombo) Commented Mar 23, 2017 at 12:53

1 Answer 1

2

You are using the same variable for the fetched database row as your array. So the array is overwritten by the new row at every iteration.

Try defining your array outside of the loop and using a different name.

$array = [];
while ($nextday = odbc_fetch_array($nextdayinfo)) {
    $username = $nextday['user_sign'];
    if (isset($username)) {
        $array[] = [
            'value' => $username, 
            'text' => $username
        ];
    }
}

print_r($array);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.