Bebus
Bebus

Reputation: 15

php function returns the last row after foreach loop

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?

Upvotes: 0

Views: 1246

Answers (3)

veb
veb

Reputation: 105

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.

Upvotes: 0

Ole Haugset
Ole Haugset

Reputation: 3797

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

Upvotes: 0

John Conde
John Conde

Reputation: 219874

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.

Upvotes: 5

Related Questions