2

Can anyone explain me how to get the final value assigned to variable after completing executing while loop?

In my below code I wanted to echo the response out of while loop after fetching all the values from the rows.

Because if I put echo out of while loop it only shows 1st record.

while ($row = oci_fetch_array($array)) {
    $response = $row['0']->load();
    echo $response;
}
1
  • You will get the last rows value into the $response, Cause: Every time the loop executes will assign value into the variable. But as you echo it you can see all the values as output. Commented Oct 2, 2016 at 7:36

4 Answers 4

3

You will get the last rows value into the $response.

Cause: Every time the loop executes will assign value into the variable. But as you echo it you can see all the values as output.

So what you really need to do is storing the values in an array...

$response = array();
while ($row = oci_fetch_array($array)) {
      $response[] = $row['0']->load();
}

print_r($response);

If you need further information about this , just let me know.

Sign up to request clarification or add additional context in comments.

1 Comment

you can consider to use oci_fetch_all instead oci_fetch_array to not need to do while loop
2

Since you are doing variable assignment and echo inside while loop, it will not serve your purpose.

You have to do it like below:-

$response = array(); // create an array
while ($row = oci_fetch_array($array)) {
      $response[] = $row['0']->load(); // assign each value to array
}

echo "<pre/>";print_r($response);// print the array

Note:- this array will have all the values now. You can manipulate it in your desired way.Thanks

Comments

0
while ($row = oci_fetch_array($array)) {
   $response = $row['0']->load();
}
echo $response;

2 Comments

This is returning only 1st row.
what is echoing the echo inside the loop? and what should be echoign? copy and paste it
0

Try this:

$response = [];
while ($row = oci_fetch_array($array)) {
  $response[] = $row['0']->load();
}

var_dump($response);

2 Comments

This is returning only 1st row.
You have added $row['0']. It will loop till the end and then will return the row's first value cause key is 0

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.