5

I have a database table for User in that i have two fields USER_ID and USER_DESCRIPTION, If i run the bellow code i get array in the form.

Array ( [USER_ID] => 1 [USER_DESCRIPTION] => TAB ) 

But i want to access those value in index based like 0, 1.How to i get that.

while (($result = oci_fetch_array($data, OCI_ASSOC)) != false) {
    echo $result['USER_ID']. ' - ' .$result['USER_DESCRIPTION']; //This works
    echo $result[0]. ' - ' .$result[1]; //This is how i want to access the values
}
1

3 Answers 3

7

You have passed second parameter OCI_ASSOC to oci_fetch_array() which will fetch only associative array.

If you change that parameter to OCI_BOTH, it will return both numeric as well associative array.

OCI_BOTH is default. So, even you can put that parameter empty.

Change

while (($result = oci_fetch_array($data, OCI_ASSOC)) != false) {

To

while (($result = oci_fetch_array($data, OCI_BOTH)) != false) {

OR To (as OCI_BOTH is default):

while (($result = oci_fetch_array($data)) != false) {

Read it here:

http://php.net/manual/en/function.oci-fetch-array.php

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

1 Comment

Hi @krishnamurali, if my answer worked for you, please mark it as solution.
4

You can try this -

$result= array_values($result); // Array with indexes you need

Or you can do another trick (assuming you are having those indexes dynamically) -

$keys = array(
    0 => 'USER_ID',
    1 => 'USER_DESCRIPTION',
);

while (($result = oci_fetch_array($data, OCI_ASSOC)) != false) {
    echo $result[$keys[0]]. ' - ' .$result[$keys[1]];
}

3 Comments

your answer is awesome and out of the box +1.
Didn't work with oci before so came out with this. :)
That is quality. You are giving a generic solution without actually working on the technology.
3

This works for me.

while (($result = oci_fetch_array($data, OCI_NUM)) != false){}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.