connect DBAPI
- Разработчикам
- DBAPI
- connect
Поддержать: USDT TRC20: TBGKTYDs4yzU17vQbobbUB8epFFtFb6PKh
3813
connect DBAPI
void connect(string $host, string $dbase, string $uid, string $pwd, boolean $persist)
$host - сервер для соединения
$dbase - выбор рабочей базы
$uid - логин
$pwd - пароль
$persist - сохранять соединение активным
Эта функция также отслеживает время, затраченное на соединение, и добавляет его к общему времени запросов.
При неудачном соединении сообщает об ошибке и завершает работу.
Пример
//Соединение со сторонней базой
$modx->db->connect('123.45.6.7', 'mydb', 'user', 'password', true);
$res = $modx->db->select('*', 'this_table');
while($tmp = $modx->db->getRow($res, 'assoc')) {
// обработка полученных данных
}
// Отключение
$modx->db->disconnect()
// Повторное подключение
$modx->db->connect();
При необходимости, можно создать отдельный экземпляр объекта и организовать подключение к отдельной базе без привязки к $modx.
Источник Функции
Файл: manager/includes/extenders/dbapi.mysql.class.inc.php
Строка: 79
function connect($host = '', $dbase = '', $uid = '', $pwd = '', $persist = 0) {
global $modx;
$uid = $uid ? $uid : $this->config['user'];
$pwd = $pwd ? $pwd : $this->config['pass'];
$host = $host ? $host : $this->config['host'];
$dbase = $dbase ? $dbase : $this->config['dbase'];
$charset = $charset ? $charset : $this->config['charset'];
$tstart = $modx->getMicroTime();
if (!$this->conn = ($persist ? mysql_pconnect($host, $uid, $pwd) : mysql_connect($host, $uid, $pwd, true))) {
$modx->messageQuit("Failed to create the database connection!");
exit;
} else {
// remove the `` chars
$dbase = str_replace('`', '', $dbase);
if (!@ mysql_select_db($dbase)) {
$modx->messageQuit("Failed to select the database '" . $dbase . "'!");
exit;
}
@mysql_query("SET CHARACTER SET {$charset}");
$tend = $modx->getMicroTime();
$totaltime = $tend - $tstart;
if ($modx->dumpSQL) {
$modx->queryCode .= ""; } $this->isConnected = true; // FIXME (Fixed by line below): // this->queryTime = this->queryTime + $totaltime; $modx->queryTime += $totaltime; } }