mysqli :: query():在第一行foreach循环后无法获取mysqli

I have created a foreach loop to add data to a MySQL database and I am receiving the error "mysqli::query(): Couldn't fetch mysqli" after the first line has been added to the database.

PHP DB CONNECTION

$db = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
} 

I then have another chunk of script which collects the data I require. The data is then added to the foreach insert loop

PHP FOREACH

foreach($RSS_DOC->channel->item as $RSSitem)
{

    $item_id    = md5($RSSitem->title);
    $fetch_date = date("Y-m-j G:i:s"); 
    $item_title = $RSSitem->title;
    $item_date  = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));
    $item_url   = $RSSitem->link;

    echo "Processing item '" , $item_id , "' on " , $fetch_date     , "<br/>";
    echo $item_title, " - ";
    echo $item_date, "<br/>";
    echo $item_url, "<br/>";

    $sql = "INSERT INTO rssingest (item_id, feed_url, item_title, item_date, item_url, fetch_date) 
    VALUES ('" . $item_id . "', '" . $feed_url . "', '" . $item_title . "', '" . $item_date . "', '" . $item_url . "', '" . $fetch_date . "')";

    if ($db->query($sql) === TRUE) {   // <- THIS IS LINE 170
        echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . $db->error;
    }

    $db->close();

}

The first line is added to the database without a problem. The second line and every line after that one returns "mysqli::query(): Couldn't fetch mysqli on line 170".

Any ideas where I may be going wrong?

The problem may be the $db->close() inside the loop. Try closing the database after the loop.

    foreach($RSS_DOC->channel->item as $RSSitem)
{

    $item_id    = md5($RSSitem->title);
    $fetch_date = date("Y-m-j G:i:s"); 
    $item_title = $RSSitem->title;
    $item_date  = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));
    $item_url   = $RSSitem->link;

    echo "Processing item '" , $item_id , "' on " , $fetch_date     , "<br/>";
    echo $item_title, " - ";
    echo $item_date, "<br/>";
    echo $item_url, "<br/>";

    $sql = "INSERT INTO rssingest (item_id, feed_url, item_title, item_date, item_url, fetch_date) 
    VALUES ('" . $item_id . "', '" . $feed_url . "', '" . $item_title . "', '" . $item_date . "', '" . $item_url . "', '" . $fetch_date . "')";

    if ($db->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . $db->error;
    }



}
$db->close();