0
public function getActiveUsers(){
    $result = $this->con->query("SELECT * FROM `USERS` where `IsActive`=1");
    $users = array();
    while ($item = $result->fetch_assoc()) {
        $users[] = $item;
    }
    return $users;
}//End Function

This is my PHP Function I am using to get active users from my DB (of my dashboard). Most of the other functions are written like this. They are written in a single PHP file (db-function.php). But first I defined the connection in a file called DB-Connet.php.

public function connect()
    {
        require_once 'source/config/config.php';
        $this->con = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
        return $this->con;
    }

And then created the connection in the construct of DbConnet class in db-function.php.

function __construct()
    {
        require_once 'DB-Connet.php';
        $db = new DbConnect();
        $this->con = $db->connect();
        
    }

But when I try to visit the dashboard url It Displays a msg like this,

User adimn_user already has more than 'max_user_connections' active connections in ..../db-function.php on line 16.

What am I doing wrong? (My English is Bad. I am sorry. Edits are Wellcome. Thank you all.)

P.S : Is it okay if I close the connection in _destruct function of db-connect.php like this?

function __destruct() {
        $this->con->close();
  }
  • Hi, I fixed it. actually, this error was occurringbecause a "bad library" I included was making a lot of unnecessary database requests. And my server was allowing only 50 calls per user. Tip - Always make sure to optimize your backend code and double-check every one of your external libraries for bad coding. – Mahesh Sandaruwan Jul 23 '21 at 10:32

1 Answers1

-3

Always close the mysql connection if you do not need it. It saves memory and free up connection for next possible requests that might hit the Database.

NashPL
  • 461
  • 1
  • 4
  • 19
  • 1
    Do you have any reference for that information? https://stackoverflow.com/questions/20492427/when-should-i-close-a-database-connection-in-php states different – Nico Haase Nov 13 '19 at 11:09
  • I stated wrong then. You learn everyday – NashPL Nov 13 '19 at 11:27