本文整理汇总了PHP中wpdb::db_connect方法的典型用法代码示例。如果您正苦于以下问题:PHP wpdb::db_connect方法的具体用法?PHP wpdb::db_connect怎么用?PHP wpdb::db_connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wpdb
的用法示例。
在下文中一共展示了wpdb::db_connect方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: kick
/**
* kick()
*
* Kicks the database to see if the conenction is still alive and if it isn't then tries to reconnect
*
* @return true if connection alive (may have been reconnected), false otherwise (dead and couldn't be reconnected)
*
*/
public function kick()
{
// Initialize result to assume failure
$result = false;
// Use ping to check if server is still present - note will not reconnect automatically for MySQL >= 5.0.13
// and actually we don't want it to as that is bad karma
if (!mysql_ping($this->_db->dbh)) {
// Database connection appears to have gone away
pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server has gone away, attempting to reconnect.', 'it-l10n-backupbuddy'));
// Close things down cleanly (from a local perspective)
@mysql_close($this->_db->dbh);
unset($this->_db->dbh);
$this->_db->ready = false;
// And attempt to reconnect
$this->_db->db_connect();
// Reconnect failed if we have a null resource or ping fails
if (NULL == $this->_db->dbh || !mysql_ping($this->_db->dbh)) {
// Reconnection failed, make sure user knows
pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server reconnection failed.', 'it-l10n-backupbuddy'));
// Make sure failure is notified (no need to close things down locally as it's a wrap anyway)
$result = false;
} else {
// Reconnection successful, make sure user knows
pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server reconnection successful.', 'it-l10n-backupbuddy'));
$result = true;
}
} else {
// Just to let user know that database is still connected
pb_backupbuddy::status(self::STATUS_TYPE_DETAILS, __('Database Server connection status verified.', 'it-l10n-backupbuddy'));
$result = true;
}
return $result;
}
示例2: db_connect
/**
* Figure out which db server should handle the query, and connect to it
*
* @param string query
*
* @return resource MySQL database connection
*/
public function db_connect($query = '')
{
if (empty($query)) {
return false;
}
$this->last_table = $this->table = $this->get_table_from_query($query);
// Use current table with no callback results
if (isset($this->ludicrous_tables[$this->table])) {
$dataset = $this->ludicrous_tables[$this->table];
$this->callback_result = null;
// Run callbacks and either extract or update dataset
} else {
// Run callbacks and get result
$this->callback_result = $this->run_callbacks('dataset', $query);
// Set if not null
if (!is_null($this->callback_result)) {
if (is_array($this->callback_result)) {
extract($this->callback_result, EXTR_OVERWRITE);
} else {
$dataset = $this->callback_result;
}
}
}
if (!isset($dataset)) {
$dataset = 'global';
}
if (empty($dataset)) {
return $this->bail("Unable to determine which dataset to query. ({$this->table})");
} else {
$this->dataset = $dataset;
}
$this->run_callbacks('dataset_found', $dataset);
if (empty($this->ludicrous_servers)) {
if ($this->dbh_type_check($this->dbh)) {
return $this->dbh;
}
if (!defined('DB_HOST') || !defined('DB_USER') || !defined('DB_PASSWORD') || !defined('DB_NAME')) {
return $this->bail('We were unable to query because there was no database defined.');
}
// Fallback to wpdb db_connect method.
$this->dbuser = DB_USER;
$this->dbpassword = DB_PASSWORD;
$this->dbname = DB_NAME;
$this->dbhost = DB_HOST;
parent::db_connect();
return $this->dbh;
}
// Determine whether the query must be sent to the master (a writable server)
if (!empty($use_master) || $this->srtm === true || isset($this->srtm[$this->table])) {
$use_master = true;
} elseif ($is_write = $this->is_write_query($query)) {
$use_master = true;
if (is_array($this->srtm)) {
$this->srtm[$this->table] = true;
}
// Detect queries that have a join in the srtm array.
} elseif (!isset($use_master) && is_array($this->srtm) && !empty($this->srtm)) {
$use_master = false;
$query_match = substr($query, 0, 1000);
foreach ($this->srtm as $key => $value) {
if (false !== stripos($query_match, $key)) {
$use_master = true;
break;
}
}
} else {
$use_master = false;
}
if (!empty($use_master)) {
$this->dbhname = $dbhname = $dataset . '__w';
$operation = 'write';
} else {
$this->dbhname = $dbhname = $dataset . '__r';
$operation = 'read';
}
// Try to reuse an existing connection
while (isset($this->dbhs[$dbhname]) && $this->dbh_type_check($this->dbhs[$dbhname])) {
// Find the connection for incrementing counters
foreach (array_keys($this->db_connections) as $i) {
if ($this->db_connections[$i]['dbhname'] == $dbhname) {
$conn =& $this->db_connections[$i];
}
}
if (isset($server['name'])) {
$name = $server['name'];
// A callback has specified a database name so it's possible the
// existing connection selected a different one.
if ($name != $this->used_servers[$dbhname]['name']) {
if (!$this->select($name, $this->dbhs[$dbhname])) {
// this can happen when the user varies and lacks permission on the $name database
if (isset($conn['disconnect (select failed)'])) {
++$conn['disconnect (select failed)'];
} else {
//.........这里部分代码省略.........
示例3: restoreWpdbConnection
private function restoreWpdbConnection()
{
if (!class_exists('wpdb')) {
return;
}
/** @var \wpdb $wpdb */
global $wpdb;
if (empty($wpdb)) {
$wpdb = new \wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
} else {
$wpdb->db_connect();
$wpdb->check_connection();
}
/**
* To avoid `mysqli_free_result` errors during the `wpdb::flush()` method set the result to `null`.
* See `wp-db.php` file line 1425.
*/
$reflection = new \ReflectionClass($wpdb);
$resultProperty = $reflection->getProperty('result');
$resultProperty->setAccessible(true);
$resultProperty->setValue($wpdb, null);
$resultProperty->setAccessible(false);
}