📜 ⬆️ ⬇️

Alternative mysql_pconnect for the mysqli driver in php 5.3

I used mysqli as a driver for connecting to the database. The problem began when I added to my test server> 500,000 records in one table. The connection with the base began to take from 1 to 10 seconds, despite the fact that the settings were:
$db['default']['pconnect'] = TRUE; 

which should have meant that the connection is through a permanent connection.

A bit of theory


mysql_pconnect does the same thing as mysql_connect, with two important exceptions:

Decision


The case turned out to be this. File /system/database/drivers/mysqli/mysqli_driver.php. The script ignores the “flag” of the permanent connection and makes the connection through the usual mysqli_connect function:
 function db_pconnect(){ $this->db_connect(); } 


Mysqli does not have a mysqli_pconnect function. Permanent connections are organized there in another way. Answer from php.net:
 To open a persistent connection you must prepend p: to the hostname when connecting. 

You just need to add “p:” before the host name. Rewrite db_pconnect () function
')
 function db_pconnect(){ $this->hostname = 'p:'.$this->hostname; $this->db_connect(); } 


Comment


This solution will work no lower than PHP 5.3.
Rewriting functions in the core of the framework is not a rewarding business, but the solution is very quick and simple. If there is a more elegant solution with the ability to extend this driver class, then please in the comments.

Links


The mysqli Extension and Persistent Connections

Source: https://habr.com/ru/post/129482/


All Articles