PHP Mysqli database connection pooling to avoid maximum user count
There is no connection pooling in PHP.
The persistent connection you are trying to use (that "p:" bit) is the most sure way to hit the maximum connection number, so get rid of it immediately.
15 concurrent connections is, actually, A LOT. Simply optimize your queries, so a typical connection would last 0.1 second, which would mean 150 concurrently executed PHP scripts which is like 1500 users on-line.
Also, you need to add
static $connect;
as the first line in your function, or is will create the new connection every time it is called, which is the actual reason why you are getting this error.
<?php
function getConnection(){
static $connect;
if (!$connect){
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connect = new mysqli('xxxx','yyy','zzz','aaaa');
$connect->set_charset('utf8mb4');
}
return $connect;
}
Better yet, get rid of this function at all, have a separate file with the mysql connection code, and then use the $connect
variable all around your code. See my article about mysqli connect for the details.
How to establish PHP PDO connection with pooling
As far as i remember from old days of programming in PHP, You cannot have pooling in PHP as php is not threded (most of times) and each requests goes line by line. In JavaScript (node.js) you can do pooling, but not on PHP as each request is one thread.
Related Topics
Get Specific Columns Using "With()" Function in Laravel Eloquent
How to Add/Remove Pkcs7 Padding from an Aes Encrypted String
How to Bind MySQLi Bind_Param Arguments Dynamically in PHP
Correctly Determine If Date String Is a Valid Date in That Format
How to Perform Static Code Analysis in PHP
Selecting a CSS Class With Xpath
MySQLi_Real_Escape_String() Expects Exactly 2 Parameters, 1 Given
Get PHP to Stop Replacing '.' Characters in $_Get or $_Post Arrays
How to Measure the Speed of Code Written in PHP
Download File Through an Ajax Call PHP
How to Set Http Header to Utf-8 Using PHP Which Is Valid in W3C Validator
Why Is the Hash Part of the Url Not Available on the Server Side
Deny Direct Access to a Folder and File by Htaccess
How to Push Both Value and Key into PHP Array
Login to Remote Site With PHP Curl