0

The following function returns only the last row of an array:

function myFunc () {

            $sql = mySql(); 
            $stid = oci_parse(getConnect(),$sql);

// runs the query above                   
oci_execute($stid);

if (oci_execute($stid)) {
            while ($row =oci_fetch_array($stid,OCI_ASSOC+OCI_RETURN_NULLS)) {
                   $out1 = "";
                   foreach($row as $column =>$entry)
                           $out1 .= $entry;
                   $output = $out1;         
                   //var_dump($output); - here I can see all array elements                                                               
                                                                            }   
 return($output);
}                       
else return "No Oracle connection";
}

var_dump() shows all the array elements, but the function displays only the last row of the array. Is it because function's return? Do I have to return an array to get all array elements? How can I get all array elements in a one string?

0

3 Answers 3

5

You override $output in each loop iteration. You need to store those values in an array (or append them depending on what you ultimately want):

$output = array();
while ($row = oci_fetch_array($stid,OCI_ASSOC+OCI_RETURN_NULLS)) {
    $out1 = "";
    foreach($row as $column =>$entry) {
        $out1 .= $entry;                                                          
    }   
    $output[] = $out1; 
}         
return($output);

This function is kinda convoluted and I'm pretty sure this can be greatly simplified, starting with the query.

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

3 Comments

Missing open { on foreach loop
Thank you. ironically I probably did that while trying to clean up the code.
Thanks, I've tried your code...and now I get the first array item displayed...var_dump($output) shows all elements, but function returns only 1 element.
0

I would have used one of these two, depending on how you would like your returned array formatted.

function myFunc () {
    $sql = mySql(); 
    $stid = oci_parse(getConnect(),$sql);             
    oci_execute($stid);
    $output = array();
    if (oci_execute($stid)) {
        for( $i = 0; $r = oci_fetch_array( $stid, OCI_ASSOC+OCI_RETURN_NULLS ) ){
            $output[$i] = $r;
        }
    }   
    return $output;
}            

//this puts the entire result array in an array. Which if the result contains of several rows will result in a multiple array result.

function myFunc () {
    $sql = mySql(); 
    $stid = oci_parse(getConnect(),$sql);             
    oci_execute($stid);
    $output = array();
    if (oci_execute($stid)) {
        while( $row = oci_fetch_array( $stid, OCI_ASSOC+OCI_RETURN_NULLS ) ){
            foreach( $row AS $key => $val ){
                $output[] = $val;
            }
        }
    }   
    return $output;
} 

// This will put every value as an array object in the $output array

Comments

0

I agree completely with John.

He was however, quicker than I.

But here's mine:

function myFunc() {
    $sql    = mySql(); 
    $stid   = oci_parse(getConnect(), $sql);

    // runs the query above                   
    $result = oci_execute($stid);

    if ($result) {
       $arr = array();
       while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
            foreach($row as $column => $entry) {
                $arr[] = $entry;
            }                                                        
        } 
        $output = $arr; // you can simply go return($arr) but I've left it like this for debugging if you need
        return($output);
    } else {
        return "No Oracle connection: " . $result;
    }
}

What I've done is added the $result of oci_execute() to give you the opportunity to debug, if the connection breaks. I've also turned your appended string into an array instead.

Hope it helps.

4 Comments

thanks for the suggested solution, but function still returns only 1 row. I don't know why...var_dump shows all elements. but when the function is called, then only 1 item is displayed..
Take out the foreach loop completely and try print_r($row);
veb, print_r($row) works well. I can see the whole $row array.
@Bebus - then you may not need the foreach loop? Try instead: $arr[] = $row;- it's messy, but might do the trick.

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.