/** * Convenience method to prepare and execute a query * * <b>Note:</b> On failure, and if the $failDie parameter is set to TRUE, this * function sends a mail to the administrator with some relevant information * such as the debug information if the * {@link EasySCP_Exception_Writer_Mail writer} is active. * * @throws EasySCP_Exception_Database * @param EasySCP_Database $db EasySCP_Database Instance * @param string $query SQL statement * @param string|int|array $bind Data to bind to the placeholders * @param boolean $failDie If TRUE, throws an EasySCP_Exception_Database exception * on failure * @return EasySCP_Database_ResultSet Return a EasySCP_Database_ResultSet object * that represents a result set or FALSE on failure if $failDie is set to FALSE */ function exec_query($db, $query, $bind = null, $failDie = true) { if (!($stmt = $db->prepare($query)) || !($stmt = $db->execute($stmt, $bind))) { if ($failDie) { throw new EasySCP_Exception($db->getLastErrorMessage() . " - Query: {$query}"); } } return $stmt; }
/** * Establishes the connection to the database * * This methods establishes the default connection to the database by using * configuration parameters that come from the basis configuration object * and then, register the {@link EasySCP_Database} instance in the * {@link EasySCP_Registry} for shared access. * * A PDO instance is also registered in the registry for shared access. * * @throws EasySCP_Exception * * @return void * @todo Remove global variable */ protected function _initializeDatabase() { try { $connection = EasySCP_Database::connect($this->_config->DATABASE_USER, decrypt_db_password($this->_config->DATABASE_PASSWORD), $this->_config->DATABASE_TYPE, $this->_config->DATABASE_HOST, $this->_config->DATABASE_NAME); } catch (PDOException $e) { throw new EasySCP_Exception('Error: Unable to establish connection to the database! ' . 'SQL returned: ' . $e->getMessage()); } // Register both Database and PDO instances for shared access EasySCP_Registry::set('Db', $connection); EasySCP_Registry::set('Pdo', EasySCP_Database::getRawInstance()); // @todo remove the Global $GLOBALS['sql'] = EasySCP_Registry::get('Db'); }