'mysql calls from PHP server works intermittently (only after user log-in) [duplicate]

I have a windows-based Apache server (XAMPP) with mysql db connections implemented using PHP. I get this error when the system tries to connect to mysql locally or remotely. Interestingly, when I login to the server, it works just fine for the next couple of hours, then issue is back awaiting for another login. XAMPP is configured as service and the user is system..

SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Here is the code I am using

$dbConnection = NULL;
try{
    $dbConnection = new PDO("mysql:host=10.100.10.100; dbname=dbName", 'username', 'password');
    $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $statement = $dbConnection->prepare("SELECT * from `table` WHERE `date` = '2022-01-31';");
    $statement->execute();
    $PendingRequest = $statement->fetchAll(PDO::FETCH_ASSOC);                
    $dbConnection = NULL;


    return json_encode(["status"=>"Good", "data" => $PendingRequest], true);
}
catch (Exception $e) {

    $dbConnection = NULL;
    return json_encode(["status"=>"error","data"=>"", "message"=>$e->getMessage()], true);
}

I appreciate any comments on this issue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source