If this function is unable to reconnect, it will forcibly die, or if after the
the template_redirect hook has been fired, return false instead.
If $allow_bail is false, the lack of database connection will need
to be handled manually.
public check_connection ( boolean $allow_bail = true ) : boolean | ||
$allow_bail | boolean | Optional. Allows the function to bail. Default true. |
Результат | boolean | True if the connection is up. |
/** * Display Settings page of the module. */ public function data_collector_page() { $dc_hostname = get_option('ltg_q_dc_hostname', ''); $dc_port = get_option('ltg_q_dc_port', ''); $dc_database = get_option('ltg_q_dc_database', ''); $dc_login = get_option('ltg_q_dc_login', ''); $dc_password = get_option('ltg_q_dc_password', ''); // Connect to the data collector server $dc_con = new wpdb($dc_login, $dc_password, $dc_database, $dc_hostname . ':' . $dc_port); // Check connection to the server if (!$dc_con->check_connection(false)) { echo '<div class="wrap"> <h2>Data Collector</h2> <p>The connection to the database cannot be established. Please update the <a href="admin.php?page=ltg-questionnaires">settings of the Data Collector</a>.</p> </div>'; exit; } include_once dirname(__FILE__) . '/partials/ltg_questionnaires-data-collector.php'; }
* THIS FILE IS ONLY FOR DEVELOPMENT PURPOSES. * IT POSES A LOT OF SECURITY RISKS (e.g. SQL INJECTION) * DO NOT USE ON PRODUCTION ENVIRONMENTS! */ // @TODO: Use WP functions to detect if the visitor is logged in or not. // Bootstrap Wordpress require_once '../../../wp-load.php'; $dc_hostname = get_option('ltg_q_dc_hostname', ''); $dc_port = get_option('ltg_q_dc_port', ''); $dc_database = get_option('ltg_q_dc_database', ''); $dc_login = get_option('ltg_q_dc_login', ''); $dc_password = get_option('ltg_q_dc_password', ''); // Connect to the data collector server $dc_con = new wpdb($dc_login, $dc_password, $dc_database, $dc_hostname . ':' . $dc_port); // Check connection to the server if (!$dc_con->check_connection(false)) { $return = array('error' => 'Database connection problems.'); echo json_encode($return); exit; } $resource = $_GET['resource']; if (function_exists($_GET['resource'])) { $op = $_GET['op'] ? $_GET['op'] : 'r'; $resource(); } function answerfieldtypes() { global $dc_con, $op; switch ($op) { case 'r': $order = $_GET['order'] ? $_GET['order'] : 'answerfieldtype_id';
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); }