There is a file called function.php and I am calling it into all my website's pages with require_once. This file contains more than hundred functions.
I get max_user_connections error from mysqli_connect() and right after that
mysqli_query() expects parameter 1 to be mysqli, boolean given
error shown when there are too many visitors or search engine bots are crawling fast.
The function file is like this:
<?php
function db_connect(){
$link=mysqli_connect("host","dbuser","dbpass","dbname");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($link,"set names 'utf8'");
}
db_connect();
function func_1(){
$result=mysqli_query($link,"select * from ...");
...
}
function func_2(){
$result=mysqli_query($link,"select * from ...");
...
}
.
.
.
function func_100(){
$result=mysqli_query($link,"select * from ...");
...
}
?>
In the end I close the database connection. As I have checked max connection per user for my database is 50 and host provider will not change it (It's enough, they said).
So, generally how can I handle this problem ? (simple PHP or OOP)