WHERE NOT IN using an Array
Try for the first query:

$notarr = db_select('uc_packages', 'p')
           -&gt;fields('p', array('package_id'))
           -&gt;condition('p.order_id', $order_id)
           -&gt;condition('p.sid', NULL)
           -&gt;execute()
           -&gt;fetchCol();


That way, you'll get the array of package_id, without needing the additional foreach, and avoid the stdClass string conversion error

Presumably also query 2 should be checking package_id instead