MysqlConnector

namespace

bhenk\msdata\connector

predicates

Cloneable | Instantiable

Static wrapper around a mysqli instance

Convenience class for a mysqli connection and configuration. Configuration options can be expressed in a configuration file. The configuration file can be set with the method MysqlConnector::setConfigFile or you can rely on auto-finding of the file.

If relying on auto-finding of configuration, this class will look for a configuration file with the name msd_config.php in a directory with the name config. The config directory should be a child of an ancestor directory of this code base:

{ancestor directory}/config/msd_config.php

The configuration file should return configuration options as an array:

<?php

return [
    "hostname" => {string},     // required
    "username" => {string},     // required
    "password" => {string},     // required
    "database" => {string},     // required
    "port" => {int},            // optional, default 3306
    "persistent" => {bool},     // optional, default true
    "use_parameterized_queries" => {bool} // optional, default true
];

A connection via libmysqlclient will not allow binding parameters in execute and produce the following error:

ArgumentCountError: Binding parameters in execute is not supported with libmysqlclient

In that case set use_parameterized_queries to false.

A third method of setting the configuration is by programmatically calling MysqlConnector::setConfiguration with the appropriate array, like shown above.


Constants

MysqlConnector::CONFIG_DIR

predicates

public

Name of the directory where a configuration file is expected

string(6) "config"

MysqlConnector::CONFIG_FILE

predicates

public

Name of the expected configuration file

string(14) "msd_config.php"

Methods

MysqlConnector::get

predicates

public | static

Get the singleton instance of this class

public static function get(): MysqlConnector

MysqlConnector::closeConnection

predicates

public | static

Close the connection (if any)

public static function closeConnection(): void
return void

MysqlConnector::getConfigFile

predicates

public

Get the (absolute path to the) configuration file

public function getConfigFile(): string|bool
return string | bool - absolute path to configuration file or false if not set

MysqlConnector::setConfigFile

predicates

public

Set the configuration file

When not using auto-find of configuration, this method must be called before a call to MysqlConnector::getConnector.

public function setConfigFile(
      Parameter #0 [ <required> string|bool $config_file ]
 ): void
param string | bool $config_file - absolute path to a configuration file, or false when returning to auto-find configuration
return void

MysqlConnector::statusInfo

predicates

public

Returns status info

Something like

Uptime: 80984
Threads: 2
Questions: 1327
Slow queries: 0
Opens: 432
Flush tables: 3
Open tables: 274
Queries per second avg: 0.016"
public function statusInfo(): string|bool
return string | bool - a string describing the server status, false if an error occurred
throws Exception

MysqlConnector::getConnector

predicates

public

Get the connector

public function getConnector(): mysqli
return mysqli - connector to database
throws Exception - if connection could not be established, code 100

MysqlConnector::useParameterizedQueries

predicates

public

The value of the configuration option use_parameterized_queries

public function useParameterizedQueries(): bool
return bool - default true
throws Exception

MysqlConnector::getConfiguration

predicates

public

Get the configuration

If configuration not set, the array will be read from the configuration file, either from the configuration file as given with MysqlConnector::setConfigFile or from the auto-find configuration file at

{ancestor directory}/config/msd_config.php
public function getConfiguration(): array
return array - configuration array
throws Exception - if configuration could not be read

MysqlConnector::setConfiguration

predicates

public

Set configuration as an array

see also

MysqlConnector

public function setConfiguration(
      Parameter #0 [ <required> array $configuration ]
 ): void
param array $configuration - configuration as described in comment on this class
return void
throws Exception - if given configuration is not valid

MysqlConnector::connectionInfo

predicates

public

Returns client and server info

Something like

client: mysqlnd 8.2.1
server: 8.0.32
host: 127.0.0.1 via TCP/IP
protocol version: 10
character set: utf8mb4
public function connectionInfo(): string
return string - client and server info
throws Exception

Sat, 01 Jul 2023 13:02:23 +0000