0

I'm running this function which is selecting data and I"m having it thrown into another function for inserting but upon errors and doing a var_dump of my results (which successfully dumped an array) I'm getting "illegal string offset" for each column"

Even though I'm casting these values, my array is basically printing each values as string(1) "number" or string(10) "value" so I know that I don't have the column names as indexes here.

What am I doing wrong?

edit: error message PHP Warning: Illegal string offset 'QT' in line 38

public  function ref()
{

    $sql = "select cast(co as DECIMAL) as co, 
                    cast(sl as DECIMAL)as sl, 
                    cast(pr as character(10)) as pr, 
                    cast(qt as decimal) as qt 
            FROM tableOne";
    $results = odbc_exec($this->DB2Conn, $sql);

    $log='';
    if ($results) {
        while($row = odbc_fetch_array($results)) {
            foreach($row as $r){
                $res = $this->add($r['sl'], $r['pr'], $r['qt'], $r['co']);
            }
        }
    }
    return $log;
}
0

2 Answers 2

1

Your inner foreach is not needed

 if ($results) {
    while($r = odbc_fetch_array($results)) {
        $res = $this->add($r['sl'], $r['pr'], $r['qt'], $r['co']);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here you load a result into $row as array():

while($row = odbc_fetch_array($results)) {

And here you iterate through $row ...

foreach($row as $r){

... so that here $r is a scalar and not an array.

$res = $this->add($r['sl'], $r['pr'], $r['qt'], $r['co']);

Possible solution (too few details to be exact):

while($row = odbc_fetch_array($results)) {
        $res = $this->add($row['sl'], $row['pr'], $row['qt'], $row['co']);
}

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.