public function connect($connection) { if (empty($connection)) { throw new Exception("Could not connect to any database without reequired connection data"); } else { $this->connection = pg_connect($connection); $this->conn_stat = pg_connection_status($this->connection); if ($this->conn_stat != PGSQL_CONNECTION_OK) { throw new Exception("Could not connect to database... :("); } } }
protected function _connect() { $connstr = ''; foreach ($this->_config as $param => $value) { if ($value) { switch ($param) { case 'host': $connstr .= "host={$value} "; break; case 'database': $connstr .= "dbname={$value} "; break; case 'port': $connstr .= "port={$value} "; break; case 'username': $connstr .= "user={$value} "; break; case 'password': $connstr .= "password={$value} "; break; } } } if (isset($this->_config['persistent'])) { $this->_connection = pg_pconnect($connstr); } else { $this->_connection = pg_connect($connstr); } if (pg_connection_status($this->_connection) !== PGSQL_CONNECTION_OK) { $this->_errorHandler(1, "Cconnection failed. "); } }
public static function castLink($link, array $a, Stub $stub, $isNested) { $a['status'] = pg_connection_status($link); $a['status'] = new ConstStub(PGSQL_CONNECTION_OK === $a['status'] ? 'PGSQL_CONNECTION_OK' : 'PGSQL_CONNECTION_BAD', $a['status']); $a['busy'] = pg_connection_busy($link); $a['transaction'] = pg_transaction_status($link); if (isset(self::$transactionStatus[$a['transaction']])) { $a['transaction'] = new ConstStub(self::$transactionStatus[$a['transaction']], $a['transaction']); } $a['pid'] = pg_get_pid($link); $a['last error'] = pg_last_error($link); $a['last notice'] = pg_last_notice($link); $a['host'] = pg_host($link); $a['port'] = pg_port($link); $a['dbname'] = pg_dbname($link); $a['options'] = pg_options($link); $a['version'] = pg_version($link); foreach (self::$paramCodes as $v) { if (false !== ($s = pg_parameter_status($link, $v))) { $a['param'][$v] = $s; } } $a['param']['client_encoding'] = pg_client_encoding($link); $a['param'] = new EnumStub($a['param']); return $a; }
public function connect() { if ($this->bConnected) { return $this->rConnection; } $iLevel = error_reporting(); // to suppress E_WARNING that can happen error_reporting(0); if ($this->iPersistence == 0) { // plain connect $this->rConnection = pg_connect($this->sConnString, PGSQL_CONNECT_FORCE_NEW); } else { if ($this->iPersistence == 1) { // persistent connect $this->rConnection = pg_pconnect($this->sConnString); } else { if ($this->iPersistence == PGSQL_CONNECT_FORCE_NEW) { // persistent connect forced new $this->rConnection = pg_connect($this->sConnString, PGSQL_CONNECT_FORCE_NEW); } } } // lets restore previous level error_reporting($iLevel); $iConnStatus = pg_connection_status($this->rConnection); if ($iConnStatus !== PGSQL_CONNECTION_OK) { if (is_resource($this->rConnection)) { pg_close($this->rConnection); } throw new \Exception('Unable to connect.'); } $this->bConnected = true; return $this->rConnection; }
/** * 连接数据库方法 * @access public */ public function connect($config = '', $linkNum = 0) { if (!isset($this->linkID[$linkNum])) { if (empty($config)) { $config = $this->config; } $pconnect = !empty($config['params']['persist']) ? $config['params']['persist'] : $this->pconnect; $conn = $pconnect ? 'pg_pconnect' : 'pg_connect'; $this->linkID[$linkNum] = $conn('host=' . $config['hostname'] . ' port=' . $config['hostport'] . ' dbname=' . $config['database'] . ' user='******'username'] . ' password='******'password']); if (0 !== pg_connection_status($this->linkID[$linkNum])) { E($this->error(false)); } //设置编码 pg_set_client_encoding($this->linkID[$linkNum], $config['charset']); //$pgInfo = pg_version($this->linkID[$linkNum]); //$dbVersion = $pgInfo['server']; // 标记连接成功 $this->connected = true; //注销数据库安全信息 if (1 != C('DB_DEPLOY_TYPE')) { unset($this->config); } } return $this->linkID[$linkNum]; }
public function getAttribute($attribute, &$source = null, $func = 'PDO::getAttribute', &$last_error = null) { switch ($attribute) { case PDO::ATTR_AUTOCOMMIT: return $this->autocommit; break; case PDO::ATTR_CLIENT_VERSION: $ver = pg_version($this->link); return $ver['client']; break; case PDO::ATTR_CONNECTION_STATUS: if (pg_connection_status($this->link) === PGSQL_CONNECTION_OK) { return 'Connection OK; waiting to send.'; } else { return 'Connection BAD'; } break; case PDO::ATTR_SERVER_INFO: return sprintf('PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s', pg_get_pid($this->link), pg_client_encoding($this->link), pg_parameter_status($this->link, 'is_superuser'), pg_parameter_status($this->link, 'session_authorization'), pg_parameter_status($this->link, 'DateStyle')); break; case PDO::ATTR_SERVER_VERSION: return pg_parameter_status($this->link, 'server_version'); break; default: return parent::getAttribute($attribute, $source, $func, $last_error); break; } }
function __construct($db_host, $db_user, $db_password, $db_name, $backend = "mysql") { define("DeveloperMailAddress", "*****@*****.**"); define("RegDataMySql", "([1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9])"); define("RegDataIt", "([0-3][0-9]/[0-1][0-9]/[1-2][0-9][0-9][0-9])"); define("RegTime", "([0-2][0-9]:[0-6][0-9]:[0-6][0-9])"); define("RegDataTime", "([1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-6][0-9]:[0-6][0-9])"); $this->ReportHeader = ""; $this->ReportFields = ""; $this->BackEnd = $backend; $this->ResultStatus = ""; $this->LastID = ""; $this->SaveErrors = ""; switch ($backend) { case "mysql": if (!($this->db = mysqli_connect($db_host, $db_user, $db_password))) { $this->StampaErr("mysql_connect", "Errore durante la connessione al database," . " verificare i parametri."); exit; } if (!mysqli_select_db($this->db, $db_name)) { $this->StampaErr("mysql_select_db", "Errore durante la selezione del database," . " verificare i parametri."); exit; } break; case "postgresql": if (!($this->db = pg_connect("host={$db_host} port=5432 dbname={$db_name} user={$db_user} password={$db_password}"))) { echo "[Debug]: pg_connection_status: " . pg_connection_status($this->db) . " <br />"; $this->StampaErr("pg_connect", "Errore durante la connessione\n o selezione del database, verificare i parametri."); exit; } break; } return $this->db; }
/** * Connect to database * * @param array $options * @return array */ public function connect($options) { $result = ['version' => null, 'status' => 0, 'error' => [], 'errno' => 0, 'success' => false]; // we could pass an array or connection string right a way if (is_array($options)) { $str = 'host=' . $options['host'] . ' port=' . $options['port'] . ' dbname=' . $options['dbname'] . ' user='******'username'] . ' password='******'password']; } else { $str = $options; } $connection = pg_connect($str); if ($connection !== false) { $this->db_resource = $connection; $this->connect_options = $options; $this->commit_status = 0; pg_set_error_verbosity($connection, PGSQL_ERRORS_VERBOSE); pg_set_client_encoding($connection, 'UNICODE'); $result['version'] = pg_version($connection); $result['status'] = pg_connection_status($connection) === PGSQL_CONNECTION_OK ? 1 : 0; $result['success'] = true; } else { $result['error'][] = 'db::connect() : Could not connect to database server!'; $result['errno'] = 1; } return $result; }
public function connect($parameters, $selectDB = false) { $this->lastParameters = $parameters; // Note: Postgres always behaves as though $selectDB = true, ignoring // any value actually passed in. The controller passes in true for other // connectors such as PDOConnector. // Escape parameters $arguments = array($this->escapeParameter($parameters, 'server', 'host', 'localhost'), $this->escapeParameter($parameters, 'port', 'port', 5432), $this->escapeParameter($parameters, 'database', 'dbname', 'postgres'), $this->escapeParameter($parameters, 'username', 'user'), $this->escapeParameter($parameters, 'password', 'password')); // Close the old connection if ($this->dbConn) { pg_close($this->dbConn); } // Connect $this->dbConn = @pg_connect(implode(' ', $arguments)); if ($this->dbConn === false) { // Extract error details from PHP error handling $error = error_get_last(); if ($error && preg_match('/function\\.pg-connect\\<\\/a\\>\\]\\: (?<message>.*)/', $error['message'], $matches)) { $this->databaseError(html_entity_decode($matches['message'])); } else { $this->databaseError("Couldn't connect to PostgreSQL database."); } } elseif (pg_connection_status($this->dbConn) != PGSQL_CONNECTION_OK) { throw new ErrorException($this->getLastError()); } //By virtue of getting here, the connection is active: $this->databaseName = empty($parameters['database']) ? PostgreSQLDatabase::MASTER_DATABASE : $parameters['database']; }
protected function is_connected() { if (!$this->db) { return false; } if (pg_connection_status($this->db) != PGSQL_CONNECTION_OK) { return false; } }
function sql_query($qry, &$conn = 0) { global $db; // If no database connection is supplied use default if (!$conn) { $conn =& $db; } if (!$conn) { print "NO DATABASE\n"; exit; } // check if connection was opened too long if (isset($conn['date']) && time() - $conn['date'] > 3600) { debug("connection {$conn['connection']} opened too long, closing", "sql"); $conn['date'] = null; pg_close($conn['connection']); unset($conn['connection']); } // check for database connection sql_connect($conn); // Rewrite SQL query call_hooks("pg_sql_query", $qry, $conn); // Do we want debug information? if (isset($conn['debug']) && $conn['debug']) { debug("CONN {$conn['title']}: " . $qry, "sql"); } // Query $res = pg_query($conn['connection'], $qry); // There was an error - call hooks to inform about error if ($res === false) { // if postgresql connection died ... if (pg_connection_status($conn['connection']) == PGSQL_CONNECTION_BAD) { debug("sql connection died", "sql"); pg_close($conn['connection']); unset($conn['connection']); call_hooks("sql_connection_failed", $conn); // if connection is back, retry query if (isset($conn['connection']) && pg_connection_status($conn['connection']) == PGSQL_CONNECTION_OK) { $res = pg_query($conn['connection'], $qry); if ($res !== false) { debug("sql retry successful", "sql"); return $res; } } else { print "sql connection died\n"; exit; } } $error = pg_last_error(); call_hooks("sql_error", $db, $qry, $error); // If we want debug information AND we have an error, tell about it if (isset($conn['debug']) && $conn['debug']) { debug("CONN {$conn['title']}: " . pg_last_error(), "sql"); } } return $res; }
function db_connect() { $uri = "host={$this->mDatabaseHost} port={$this->mDatabasePort} dbname={$this->mDatabaseName} user={$this->mDatabaseUsername} password={$this->mDatabasePassword}"; $connection = pg_connect($uri); if (pg_connection_status($connection) == PGSQL_CONNECTION_OK) { return $connection; } die('数据库访问错误!'); }
function status() { $status = pg_connection_status($this->dbConnection); if ($status === PGSQL_CONNECTION_OK) { return "Connection status ok"; } else { return "Connection status bad"; } }
public function ifConnected() { $stat = pg_connection_status($this->link); if ($stat === 0) { return true; } else { return false; } }
function db_backend_connection() { $conf = get_config(); // connect to database $db = pg_connect($conf['database']['connect']) or die('Unable to connect to db node server'); if (pg_connection_status($db) !== PGSQL_CONNECTION_OK) { die('db: Bad connection status'); } return $db; }
function do_sql($query) { // Connect to DB $dbconn = $_GLOBAL['dbconn']; if (pg_connection_status($dbconn) !== PGSQL_CONNECTION_OK) { $dbconn = pg_connect("host=localhost dbname=training_diary user=athlete password=athlete0") or die('Could not connect: ' . pg_last_error()); $_GLOBAL['dbconn'] = $dbconn; } #echo "\n<div><br>DEBUG $query <br></div> \n"; $result = pg_query($query) or die('Query failed: ' . pg_last_error()); #pg_close($dbconn); return $result; }
protected function is_connected() { if (!$this->db) { return false; } if (IFPG and pg_connection_status($this->db) != PGSQL_CONNECTION_OK) { return false; } if (IFMY) { return mysql_ping($this->db); } return true; }
function __construct($connection_string) { //echo "Opening Connection\n"; $this->pg_connection = pg_connect($connection_string); if (!$this->pg_connection) { throw new DatabaseException('Connection Error'); } if (PGSQL_CONNECTION_OK != pg_connection_status($this->pg_connection)) { throw new DatabaseException(pg_errormessage($this->pg_connection)); } // Timezone // Serialisation // Log? }
function connect() { if ($this->connection != 0) { return pg_connection_status($this->connection) == PGSQL_CONNECTION_OK; } $this->connection = pg_connect("host={$this->host} port=5432 dbname={$this->db} user={$this->user} password={$this->pass}"); if ($this->connection == 0) { return 0; } $this->queries = 0; if ($this->connection != 0) { return pg_connection_status($this->connection) == PGSQL_CONNECTION_OK; } return 0; }
/** * @coroutine * * @param string $connectionString * @param float|int $timeout * * @return \Generator * * @resolve \Icicle\Postgres\Connection * * @throws \Icicle\Postgres\Exception\FailureException */ function connect(string $connectionString, float $timeout = 0) : \Generator { if (!($connection = @\pg_connect($connectionString, \PGSQL_CONNECT_ASYNC | \PGSQL_CONNECT_FORCE_NEW))) { throw new FailureException('Failed to create connection resource'); } if (\pg_connection_status($connection) === \PGSQL_CONNECTION_BAD) { throw new FailureException(\pg_last_error($connection)); } if (!($socket = \pg_socket($connection))) { throw new FailureException('Failed to access connection socket'); } $delayed = new Delayed(); $callback = function ($resource, bool $expired) use(&$poll, &$await, $connection, $delayed, $timeout) { try { if ($expired) { throw new TimeoutException('Connection attempt timed out.'); } switch (\pg_connect_poll($connection)) { case \PGSQL_POLLING_READING: return; // Connection not ready, poll again. // Connection not ready, poll again. case \PGSQL_POLLING_WRITING: $await->listen($timeout); return; // Still writing... // Still writing... case \PGSQL_POLLING_FAILED: throw new FailureException('Could not connect to PostgreSQL server'); case \PGSQL_POLLING_OK: $poll->free(); $await->free(); $delayed->resolve(new BasicConnection($connection, $resource)); return; } } catch (\Throwable $exception) { $poll->free(); $await->free(); \pg_close($connection); $delayed->reject($exception); } }; $poll = Loop\poll($socket, $callback, true); $await = Loop\await($socket, $callback); $poll->listen($timeout); $await->listen($timeout); return (yield $delayed); }
function __construct($db_host = '10.11.11.40', $db_nombre = 'legalizacionescitas', $db_usuario = 'postgres', $db_clave = 'postgres', $db_puerto = '5432') { $this->db_host = $db_host; $this->db_usuario = $db_usuario; $this->db_clave = $db_clave; $this->db_nombre = $db_nombre; $this->db_puerto = $db_puerto; try { $this->linkid = @pg_connect('host=' . $this->db_host . ' user='******' password='******' dbname=' . $this->db_nombre . ' port=' . $this->db_puerto); if (!(@pg_connection_status($this->linkid) == "PG_CONNECTION_BAD")) { throw new Exception("No existe conexión con el servidor de PostgreSQL. " . @pg_last_error($this->linkid)); } } catch (Exception $e) { die($e->getMessage()); } }
/** * Connect to the database. * * @param bool $persistent * @return mixed */ public function connect($persistent = false) { if (empty($this->DSN)) { $this->buildDSN(); } $this->connID = $persistent === true ? pg_pconnect($this->DSN) : pg_connect($this->DSN); if ($this->connID !== false) { if ($persistent === true && pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD && pg_ping($this->connID) === false) { return false; } empty($this->schema) or $this->simpleQuery("SET search_path TO {$this->schema},public"); if ($this->setClientEncoding($this->charset) === false) { return false; } } return $this->connID; }
/** * 连接数据库方法 * @access public */ public function connect($config = '', $linkNum = 0) { if (!isset($this->linkID[$linkNum])) { if (empty($config)) { $config = $this->config; } $pconnect = !empty($config['params']['persist']) ? $config['params']['persist'] : $this->pconnect; $conn = $pconnect ? 'pg_pconnect' : 'pg_connect'; $this->linkID[$linkNum] = $conn('host=' . $config['hostname'] . ' port=' . $config['hostport'] . ' dbname=' . $config['database'] . ' user='******'username'] . ' password='******'password']); if (0 !== pg_connection_status($this->linkID[$linkNum])) { E($this->error(false)); } //设置编码 pg_set_client_encoding($this->linkID[$linkNum], $config['charset']); } return $this->linkID[$linkNum]; }
public function Connection() { $host_db = 'localhost'; $port_db = '5432'; $base = "sae_dbassignations"; $usuario = "saeweb"; $password = "******"; if (!isset($this->conexion)) { $this->conexion = pg_connect("host={$host_db} port={$port_db} dbname={$base} user={$usuario} password={$password}") or die("No se ha podido conectar"); $stat = pg_connection_status($this->conexion); if ($stat === PGSQL_CONNECTION_OK) { //echo 'Estado de la conexión ok'; } else { echo 'No se ha podido conectar'; } } }
/** * @brief DB Connection **/ function __connect($connection) { // the connection string for PG $conn_string = ""; // Create connection string $conn_string .= $connection["db_hostname"] ? ' host=' . $connection["db_hostname"] : ""; $conn_string .= $connection["db_userid"] ? " user="******"db_userid"] : ""; $conn_string .= $connection["db_password"] ? " password="******"db_password"] : ""; $conn_string .= $connection["db_database"] ? " dbname=" . $connection["db_database"] : ""; $conn_string .= $connection["db_port"] ? " port=" . $connection["db_port"] : ""; // Attempt to connect $result = @pg_connect($conn_string); if (!$result || pg_connection_status($result) != PGSQL_CONNECTION_OK) { $this->setError(-1, "CONNECTION FAILURE"); return; } return $result; }
public function getPoster($oid) { //TODO check good run $connection = $this->_em->getConnection(); $database = pg_connect("host=" . $connection->getHost() . " port=5432 dbname=" . $connection->getDatabase() . " user=postgres"); $stat = pg_connection_status($database); if ($stat !== PGSQL_CONNECTION_OK) { return null; } ob_start(); pg_query($database, "begin"); $handle = pg_lo_open($database, $oid, "r"); pg_lo_read_all($handle); pg_query($database, "commit"); $imgContent = ob_get_contents(); ob_end_clean(); return $imgContent; }
function conectaBD() { switch (_DB_TYPE) { case 'mysql': $link_id = @mysql_connect(_DB_SERVER, _DB_SERVER_USERNAME, _DB_SERVER_PASSWORD) or die(include _DOCUMENT_ROOT . "general/error_db.php"); mysql_select_db(_DB_DATABASE) or die(include _DOCUMENT_ROOT . "general/error_db.php"); break; case 'postgresql': if (isset($_SESSION["connId"])) { $stat = pg_connection_status($_SESSION["connId"]); if ($stat === 0) { return $link_id; } } $options = 'dbname=' . _DB_DATABASE . ' port=' . _DB_PORT . ' user='******' host=' . _DB_SERVER . ' password=' . _DB_SERVER_PASSWORD; $link_id = @pg_connect($options) or die(include _DOCUMENT_ROOT . "general/error_db.php"); $_SESSION["connId"] = $link_id; break; } return $link_id; }
/** +---------------------------------------------------------- * 连接数据库方法 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ public function connect($config = '', $linkNum = 0) { if (!isset($this->linkID[$linkNum])) { if (empty($config)) { $config = $this->config; } $conn = $this->pconnect ? 'pg_pconnect' : 'pg_connect'; $this->linkID[$linkNum] = $conn('host=' . $config['hostname'] . ' port=' . $config['hostport'] . ' dbname=' . $config['database'] . ' user='******'username'] . ' password='******'password']); if (pg_connection_status($this->linkID[$linkNum]) !== 0) { throw_exception($this->error(false)); } $pgInfo = pg_version($this->linkID[$linkNum]); $this->dbVersion = $pgInfo['server']; // 标记连接成功 $this->connected = true; //注销数据库安全信息 if (1 != C('DB_DEPLOY_TYPE')) { unset($this->config); } } return $this->linkID[$linkNum]; }
public function __construct(array $config = array()) { parent::__construct($config); if (isset($config['db']) && is_resource($config['db'])) { $this->db = $config['db']; } else { $config['host'] = $this->_($config, 'host', null); $config['port'] = $this->_($config, 'port', null); $config['database'] = $this->_($config, 'database', null); $config['user'] = $this->_($config, 'user', null); $config['password'] = $this->_($config, 'password', null); $config['new_link'] = (bool) $this->_($config, 'new_link', false); $connection_string = "host='" . $config['host'] . "' " . "port='" . $config['port'] . "' " . "dbname='" . $config['database'] . "' " . "user='******'user'] . "' " . "password='******'password'] . "' "; if ($this->_($config, 'persistent')) { $this->db = pg_pconnect($connection_string, $config['new_link']); } else { $this->db = pg_connect($connection_string, $config['new_link']); } if ($this->db == false) { throw new AleExceptionCache(pg_last_error(), pg_connection_status()); } } }
function DbError() { if (pg_connection_status($this->ConnectionID) != 0) { return translate("ERROR_DBCONNECT_3"); } return pg_last_error($this->ConnectionID); }