Need help on my project

Hi,
I'm working on a project using a nodemcu esp8266. the purpose is to insert in a mysql database the mac addresses and timestamp of wifi devices in an area.
my probleme is that I lose wifi connection and sql connection when sniffing.

here is my code:

#include <ESP8266WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

#define WIFI_CHANNEL_SWITCH_INTERVAL (5000)
#define WIFI_CHANNEL_MAX (15)

const char *ssid = "xxx";
const char *password = "xxx";

IPAddress server_addr(xxx, xxx, xxx, xxx); 
char user[] = "super";
char db_password[] = "xxx";
char db_name[] = "xxx";

const char *table_name = "xxx";
const char *mac_address_column = "xxx";
const char *timestamp_column = "xxx";

uint8_t mac_lib[100][6];
int mac_count = 0;

uint8_t level = 0, channel = 1;

typedef struct
{
  unsigned frame_ctrl : 16;
  unsigned duration_id : 16;
  uint8_t addr1[6]; /* receiver address */
  uint8_t addr2[6]; /* sender address */
  uint8_t addr3[6]; /* filtering address */
  unsigned sequence_ctrl : 16;
  uint8_t addr4[6]; /* optional */
} wifi_ieee80211_mac_hdr_t;

typedef struct
{
  wifi_ieee80211_mac_hdr_t hdr;
  uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */
} wifi_ieee80211_packet_t;

// Create MySQL connection object
WiFiClient client;
MySQL_Connection conn((Client *)&client);

void setup()
{
  Serial.begin(115200);
  delay(10);
WiFi.mode(WIFI_STA);
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Connect to MySQL
  if (conn.connect(server_addr, 3306, user, db_password, db_name))
  {
    Serial.println("Connected to MySQL server");
  }
  else
  {
    Serial.println("Connection to MySQL failed");
    while (1)
      ;
  }

  // Init sniffer
  wifi_sniffer_init();
}

void loop()
{
  Serial.printf("______________CHANNEL:%02d____________\n", channel);
  delay(1000);

  wifi_sniffer_set_channel(channel);
  channel = (channel % WIFI_CHANNEL_MAX) + 1;
  if (channel == 1)
  {
    Serial.println("Unique MAC Count: " + String(mac_count));
    mac_count = 0;
  }
}

void wifi_sniffer_init(void)
{
  WiFi.disconnect();
  WiFi.mode(WIFI_STA);
  wifi_set_opmode(STATION_MODE); // Set mode to station mode for ESP8266
  wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler);
  wifi_promiscuous_enable(1);
}

void wifi_sniffer_set_channel(uint8_t channel)
{
  wifi_set_channel(channel);
  delay(100);
}

void wifi_sniffer_packet_handler(uint8_t *buff, uint16_t len)
{
  const wifi_ieee80211_packet_t *ipkt = (wifi_ieee80211_packet_t *)buff;
  const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr;

  if (check_mac_only(hdr->addr3))
  {
    Serial.printf("CHAN=%02d,  ADDR3=%02x:%02x:%02x:%02x:%02x:%02x\n",
                  wifi_get_channel(),
                  /* ADDR3 */
                  hdr->addr3[0], hdr->addr3[1], hdr->addr3[2],
                  hdr->addr3[3], hdr->addr3[4], hdr->addr3[5]);

    // Insert MAC address into MySQL database
    insert_mac_address(hdr->addr3);
  }
}
int check_mac_only(const uint8_t addr3[6])
{
  for (char i = 0; i < mac_count; i++)
  {
    bool flag = true;
    for (char j = 0; j < 6; j++)
    {
      if (mac_lib[i][j] != addr3[j])
      {
        flag = false;
        break;
      }
    }
    if (flag == true)
      return 0;
  }
  for (char j = 0; j < 6; j++)
  {
    mac_lib[mac_count][j] = addr3[j];
  }
  mac_count++;
  return 1;
}
void insert_mac_address(const uint8_t addr[6])
{
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.println("WiFi connection lost. Reconnecting...");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(1000);
      Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");
  }
    // Connect to MySQL
  if (conn.connect(server_addr, 3306, user, db_password, db_name))
  {
    Serial.println("Connected to MySQL server");
  }
  else
  {
    Serial.println("Connection to MySQL failed");
    while (1)
      ;
  }
  ////////////////////////////////////////
  char query[128];
  sprintf(query, "INSERT INTO %s (%s, %s) VALUES ('%02x:%02x:%02x:%02x:%02x:%02x', NOW())",
          table_name, mac_address_column, timestamp_column,
          addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);

  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  cur_mem->execute(query);
  delete cur_mem;
}
 

Are you sure the string fits in 128 characters?
Maybe use snprintf()?
Why do you new and delete the cursor every loop?
Where is addr[] defined? (I read from phone, so it might be there,but I could not find it...).
Is addr[0] a null terminated string?

What is this supposed to do?

Why is this cast needed?