Source
* @param string $doCommit True - do commit, rollback otherwise. Rollback is also always performed if a sql failed within this transaction.
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* Creates global database connection.
*
* @param string $error returns a message in case of an error
* @param bool $debug turns On or Off trace calls when making connections. Suggested debug mode Off during Zabbix setup
*
* @return bool
*/
function DBconnect(&$error) {
global $DB;
if (isset($DB['DB'])) {
$error = _('Cannot create another database connection.');
return false;
}
$DB['DB'] = null; // global db handler
$DB['TRANSACTIONS'] = 0; // level of a nested transaction
$DB['TRANSACTION_NO_FAILED_SQLS'] = true; // true - if no statements failed in transaction, false - there are failed statements
$DB['SELECT_COUNT'] = 0; // stats
$DB['EXECUTE_COUNT'] = 0;
if (!isset($DB['TYPE'])) {
$error = 'Unknown database type.';
return false;
}
$db_types = [
ZBX_DB_MYSQL => MysqlDbBackend::class,
ZBX_DB_POSTGRESQL => PostgresqlDbBackend::class,
ZBX_DB_ORACLE => OracleDbBackend::class
];
if (!array_key_exists($DB['TYPE'], $db_types)) {
$error = 'Unsupported database';
return false;
}
$db = new $db_types[$DB['TYPE']];
if ($DB['ENCRYPTION']) {
$db->setConnectionSecurity($DB['KEY_FILE'], $DB['CERT_FILE'], $DB['CA_FILE'], $DB['VERIFY_HOST'],
$DB['CIPHER_LIST']
);
}
$DB['DB'] = $db->connect($DB['SERVER'], $DB['PORT'], $DB['USER'], $DB['PASSWORD'], $DB['DATABASE'], $DB['SCHEMA']);