SQL Query runs slower on PHP. The same query on Putty (Mysql) runs immediately

Job ID: 33307530

Budget: $10 – $20 USD

I have a table with 7954700 rows. I have optimized the SQL query to the maximum possible that it only takes 3 seconds to be solved, but executing the same query on PHP without PDO it takes about 4 minutes to be executed.
I am using Ubuntu 20, mysql 8 and PHP 8.1
Related categories: PHP Linux SQL Apache MySQL