-1

I'm having a small issue getting an index defined for use in a DB2 query in php.

The below script functions except for the portion where I'm trying to put DB2 results in an array and print them. Instead of printing the array, it prints the error shown below in /**/. Specifically, an illegal string offset of order_id in my where clause.

The mysql portion, as far as selecting and updating, works perfectly.

However, for all of the order_ids in $order_ids, I want to use that variable to match them in the db2 query and then get all of the SELECT results from DB2 in an array so that I can check to make sure the array contains the data for all of the right orders/records.

Possibly just an issue with passing the variables to DB2 but I've been doing so much with DB2 in PHP the last few days that I'm starting to have a hard time pin pointing things, since DB2 isn't something I've ever worked with.

How can I change this to use the $order_ids[] = $row['order_id'] and match it to the invnoc column in db2?

$orderShippedCheck = "
            SELECT 
                order_id,
                order_status
            FROM order_status
            WHERE order_status = 'S'
    ";

    $result = mysqli_query($mysqlConn, $orderShippedCheck);
    $order_ids = array();

    //loop results to gather order IDs and store them
    while ($row = mysqli_fetch_array($result)){
        $order_ids[] = $row['order_id'];
        }

        print_r($order_ids);

        //Update those records to show they were made placements, and update the date
        foreach($order_ids as $order_id){

            $updatePlacement = "UPDATE order_status SET is_placement = 1, date_updated = DATE(NOW()) WHERE order_id = '$order_id';";


            //SELECT FROM DB2 WITH THE ORDER NUMBERS FIRST
            $DB2Shipped = " 
                SELECT  
                    invnoc as INVOICE,
                    cstnoc AS DEALER,
                    framec AS FRAME,
                    covr1c AS COVER,
                    colr1c AS COLOR ,
                    extd2d AS SHIPDATE,
                    orqtyc AS QUANTITY
                FROM GPORPCFL
                WHERE invnoc = '{$order_id['order_id']}'
        group by invnoc,cstnoc, slsnoc, orqtyc, framec, covr1c,colr1c, extd2d
        order by invnoc asc
    ";

    print_r($order_id);

    $Db2ShipRslt = odbc_exec($DB2Conn,$DB2Shipped);
        if ( $Db2ShipRslt === false ) {
             exit (odbc_errormsg($DB2Conn));
        }

    $Db2ShipArr = array();
    print_r($order_id); /*This prints the order ID for each record*/
    print_r($Db2ShipArr); /*This prints an empty array, Array()*/

    while($db2ShipRow = odbc_fetch_array($Db2ShipRslt)){
        {
        $Db2ShipArr[] = $db2ShipRow['INVOICE'];
        }

        foreach($Db2ShipArr as $Db2Ship){
            print_r($Db2ShipArr);
        }
    }
Geoff_S
  • 4,917
  • 7
  • 43
  • 133

1 Answers1

3

I see you are looping over $order_ids:

foreach($order_ids as $order_id){

But then inside the loop, I think you mistakenly reference the toplevel array as if it is one of the rows:

WHERE invnoc = '{$order_ids['order_id']}'
/* This is where I get the error Undefined index: order_id*/

I think it should be:

WHERE invnoc = '{$order_id['order_id']}'
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Ah, I'm not sure how I missed that exactly. However, even with that change I get an illegal string offest for 'order_id' in the same spot – Geoff_S Mar 01 '18 at 17:34
  • I suggest you `print_r($order_id)` to debug. – Bill Karwin Mar 01 '18 at 17:43
  • I have ```print_r($order_id) and print_r($db2shipArr)``` and it prints this ```12605987Array ( )``` So the order ID itself but then an empty array, it looks like. – Geoff_S Mar 01 '18 at 17:51
  • Ok, so I updated the code with revisions and debugs. The first time I print_r $db2shipArr, it prints the empty array. The second one in the next foreach loop doesn't print at all, I guess because it's not making it to that point. So maybe the illegal string offset is pointing to an index issue? – Geoff_S Mar 01 '18 at 18:01
  • I'm also confused as to why the MySQL update using the same $order_id would work, so I guess that's where passing to DB2 becomes an issue – Geoff_S Mar 01 '18 at 18:04
  • Ok I figured it out! It needed ```WHERE invnoc = '{$order_id}'```. Thank you for your help – Geoff_S Mar 01 '18 at 18:18