I am writing a CLI utility in PHP. I work with MySQL through the mysqli library. I am using mysqli in object mode. The script connects to a remote MySQL server and reads a lot of information from the database (up to 500 MB). There is an urgent need to reduce the traffic between the script and the MySQL server. The most logical way to do this is to use the MYSQLI_CLIENT_COMPRESS compression connection.
The question is: how, using the mysqli class, to raise the connection through MYSQLI_CLIENT_COMPRESS? After all, the connection is raised when the object is created (in the constructor), and there is nowhere to set the compression flag.
It is set in mysqli_real_connect(), but by the time this function is used, the connection has already been initiated by the mysqli constructor.
Please don’t suggest a procedural style of working with mysqli. If there are no other options, this is obvious.
Answer 1, authority 100%
mysqli in object mode has
real_connect(...). Look at the last possible parameter – flags, through it you can pass MYSQLI_CLIENT_COMPRESS
$mysqli = new mysqli(); $mysqli->real_connect('localhost', 'root', '1', 'tmp_data', 3306, NULL, MYSQLI_CLIENT_COMPRESS)); printf("Server version: %s\n", $mysqli->server_info);
If you do not specify parameters in the constructor, then it tries to connect with default parameters, so it is advisable to use
mysqli_init()(in this case it won’t try to connect) because
mysql::init()is not a static method at all (therefore, in my opinion, it can only be called by inheriting from the mysqli class and called in the constructor as
parent::init(), although horseradish is not sweeter – the inheritor class will still have to be created through new).
mysqli_init()it would look like this:
$mysqli = mysqli_init(); $mysqli->real_connect('localhost', 'root', '1', 'tmp_data', 3306, NULL, MYSQLI_CLIENT_COMPRESS)); printf("Server version: %s\n", $mysqli->server_info);
P.S. Look towards PDO, you might like it 🙂