How to avoid PHP Warning for Undefined Offset in wp-db.php using foreach

  mysql, php, wordpress

I’m attempting to set a cookie containing a post ID, but only when the current post ID isn’t present in a table. The code works, but I’m getting a PHP warning "Undefined offset: 0" from wp-includes/wp-db.php and I’m wondering what I’ve done wrong.

function getFromDatabase() {
    global $wpdb;
    $sql = $wpdb->prepare( "SELECT page_id FROM foo WHERE page_id IS NOT NULL" );
    $result = $wpdb->get_results( $sql );
    $resultsArray = array();
    foreach ( $result as $row ) {
        $resultsArray[] = $row->page_id;
    return $resultsArray;


$currentPID = get_the_id();
$ignoredPIDs = getFromDatabase();
if ( !in_array($currentPID, $ignoredPIDs) ) {
    // Set a cookie to post ID

I should mention that the DB query will always return at least one result. Any thoughts on this would be greatly appreciated.

Source: Ask PHP