本文整理汇总了PHP中mysqli::ping方法的典型用法代码示例。如果您正苦于以下问题:PHP mysqli::ping方法的具体用法?PHP mysqli::ping怎么用?PHP mysqli::ping使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mysqli
的用法示例。
在下文中一共展示了mysqli::ping方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: isConnected
/**
* is the MySQL server connected?
* @return boolean
*/
function isConnected()
{
if (php_sapi_name() == 'fpm-fcgi') {
return $this->mysqli != null;
}
//Web requests are short
return $this->mysqli && @$this->mysqli->ping();
}
示例2: _getMysqli
/**
* connect to the db and save the connection
* @throws \Exception
* @return bool|\mysqli
*/
private function _getMysqli()
{
if (!$this->_mysqli instanceof \mysqli) {
$this->_mysqli = @new \mysqli(static::DB_HOSTNAME, static::DB_USERNAME, static::DB_PASSWORD, static::DB_DATABASE, static::DB_PORT);
if ($this->_mysqli->connect_error) {
throw new \Exception("Error connecting to database: {$this->_mysqli->connect_error}");
}
} else {
if (!$this->_mysqli->ping()) {
$this->_mysqli = null;
$this->_getMysqli();
}
}
return $this->_mysqli;
}
示例3: query
/**
* Prepares a sql query optionally as a prepared statement if the prepArgs
* array is specified
* @name query
* @param str $sql SQL to execute
* @param str $prepArgs Arguments for prepared statement queries
* @since 0.1.0
* @return object query results
* <code>
* <?php
* $query = $db->query("select * from foo")
*
* //prepared (safe from injection)
* $query = $db->query("select * from foo where foo_id = ?", ['i', 1]);
*
* ?>
* </code>
*/
public function query($sql, $prepArgs = false)
{
if (!$this->conn->ping()) {
$this->conn->close();
$this->connect();
}
try {
if (is_array($prepArgs)) {
$stmt = $this->conn->prepare($sql);
if (false === $stmt) {
$this->error("Couldn't prepare statement: " . $this->conn->error);
} else {
$method = new \ReflectionMethod('mysqli_stmt', 'bind_param');
$method->invokeArgs($stmt, $this->_mkrefs($prepArgs));
/* much love to jan kriedner */
$stmt->execute();
if ($stmt->insert_id > 0) {
$result = $stmt->insert_id;
} else {
$result = $stmt->get_result();
}
}
} else {
$result = $this->conn->query($sql);
}
} catch (Exception $e) {
$this->error($e->getMessage() . " SQL: {$sql}");
}
return $result;
}
示例4: connected
/**
* Determines if the connection to the server is active.
*
* @return boolean True if connected to the database engine.
*
* @since 1.0
*/
public function connected()
{
if (is_object($this->connection)) {
return $this->connection->ping();
}
return false;
}
示例5: measurePing
public function measurePing(&$micro)
{
$micro = -microtime(true);
$result = $this->mysqli->ping();
$micro += microtime(true);
return $result;
}
示例6: ping
public function ping()
{
if (!@$this->handler->ping()) {
return $this->reconnect();
}
return true;
}
示例7: ping
/**
* Ping a server connection or reconnect if there is no connection
* @return bool
*/
public function ping()
{
if (!$this->dbh) {
return false;
}
return @$this->dbh->ping();
}
示例8: reconnect
public function reconnect()
{
if (!$this->mysqli->ping()) {
$this->mysqli->close();
return $this->connect();
}
return true;
}
示例9: testMysqliConnection
public function testMysqliConnection()
{
$mysqliConn = new mysqli("localhost", "root", "eqBZKHCd775HA2fS", "JobGossip");
$connection = $mysqliConn->ping();
$this->assertTrue($connection);
$mysqliConn->close();
//cleanup test
}
示例10: setDatabase
public static function setDatabase(mysqli $db)
{
self::$db = null;
if ($db && $db->ping()) {
self::$db = $db;
return true;
}
return false;
}
示例11: reconnect
/**
* Reconnect to the db server
*/
public function reconnect()
{
if (isset($this->native) && @$this->native->ping()) {
return;
}
$native = new mysqli($this->settings['host'], $this->settings['user'], $this->settings['password'], $this->settings['dbname'], $this->settings['port'], $this->settings['unix_socket']);
if (!$native) {
throw new DB_Exception("Connecting to mysql database failed: " . \mysqli::connect_error());
}
$this->native = $native;
}
示例12: __destruct
/**
* Destructor: cierra la conexión a MySQL si está abierta
* @throws DBException si no se puede cerrar la conexion
*/
public function __destruct()
{
try {
$pingResult = parent::ping();
} catch (\Exception $e) {
$pingResult = false;
}
if ($pingResult) {
if (!parent::close()) {
throw new DBException(mysqli_connect_error(), mysqli_connect_errno());
}
}
}
示例13: isConnected
/**
* Is connected
*
* @return bool
*/
public function isConnected()
{
if ($this->resource instanceof \mysqli) {
// 检测连接是否有效
if (!$this->resource->ping()) {
$this->disconnect();
return false;
}
return true;
} else {
return false;
}
}
示例14: lazyConnect
/**
* If there is no DB connection established yet, it connects and populates self::$connection attribute.
*
* Also "wakes up" connection if it has gone away.
*/
protected function lazyConnect()
{
if (isset($this->connection)) {
// Connection might have gone away.
$this->connection->ping();
return;
}
list($host, $user, $password, $database) = $this->config->getMulti(array('db_host', 'db_user', 'db_password', 'db_name'));
$this->connection = new mysqli($host, $user, $password);
if (mysqli_connect_errno()) {
throw new PHPTracker_Persistence_Error('Unable to connect to mysql database: ' . mysqli_connect_error());
}
if (false === $this->connection->select_db($database)) {
throw new PHPTracker_Persistence_Error("Unable to select database: {$database}.\n" . $this->connection->error);
}
}
示例15: probarConeccion
public function probarConeccion()
{
$mysqli = new mysqli("104.236.75.102", "monty", "rioslopez", "itcVolBank");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit;
}
/* check if server is alive */
if ($mysqli->ping()) {
printf("Our connection is ok!\n");
} else {
printf("Error: %s\n", $mysqli->error);
}
/* close connection */
$mysqli->close();
}