Welcome Guest, Not a member yet? Register   Sign In
Site online with db offline
#1

I'll explain...
My site use of multiple db and they have different functions.
When one of this goes offline the entire site goes offline, showing the usual error message.
Since, however, the db set as main stays online, there is no way to run the site too? Simply without showing the contents of the secondary?
Reply
#2

How are you loading the secondary databases? What is "the usual error message"?

As far as I can tell, if you have db_debug set to false in your database config, as long as the database library doesn't find some issue with the other configuration values, you should be able to continue if the database fails to connect, as long as you don't try to use the failed database connection.
Reply
#3

I deactivate the db_debug, I turn off the database and I received this error:
Code:
A PHP Error was encountered

Severity: Warning

Message: mysqli::real_connect(): (HY000/2002): No such file or directory

Filename: mysqli/mysqli_driver.php

Line Number: 135

Backtrace:

File: /Applications/XAMPP/xamppfiles/htdocs/arcor/application/models/Ranks.php
Line: 36
Function: database

File: /Applications/XAMPP/xamppfiles/htdocs/arcor/application/core/MY_Controller.php
Line: 47
Function: players

File: /Applications/XAMPP/xamppfiles/htdocs/arcor/application/core/MY_Controller.php
Line: 11
Function: players_rapid

File: /Applications/XAMPP/xamppfiles/htdocs/arcor/index.php
Line: 292
Function: require_once

Code:
Fatal error: Call to a member function real_escape_string() on boolean in /Applications/XAMPP/xamppfiles/htdocs/arcor/system/database/drivers/mysqli/mysqli_driver.php on line 331
A PHP Error was encountered

Severity: Error

Message: Call to a member function real_escape_string() on boolean

Filename: mysqli/mysqli_driver.php

Line Number: 331

Backtrace:

The connections I make them by the manual: $DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
Reply
#4

For some reason the line numbers in those error messages don't line up for me, but I was forgetting that mysqli dumps errors on its own. If you're using mysqli for all of the connections and don't care about being able to switch to another database driver, you can try something like this:

PHP Code:
// Tell mysqli to throw exceptions instead of errors
mysqli_report(MYSQLI_REPORT_STRICT);

try {
 
   $DB1 $this->load->database('group_one'TRUE);
} catch (
Exception $e) {
 
   // $DB1 failed to connect, or some other exception 
 
   // was thrown while trying to load the database.
}

try {
 
   $DB2 $this->load->database('group_two'TRUE);
} catch (
Exception $e) {
 
   // $DB2 failed to connect, or some other exception 
 
   // was thrown while trying to load the database.

Reply
#5

Where should I put this code?

Because this two variable
DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

I am declared whenever I need that the connection is open. Not only once in a file.
Reply
#6

Before you get too far along, I would recommend just trying the code in an easy-to-test location to see if it works as you expect it to.

If it does work, you might be able to get away with putting the mysqli_report() call in your index.php file, then wrapping any call to load a database in the try/catch blocks and handling the connection errors appropriately.

In general, if you're managing multiple databases, you should probably have that management code centralized in a library or model so you don't have the same code in multiple locations, but the best way to deal with this is really dependent on the structure of your application and how you are using the database connections. The important part is to make sure that you don't try to use a particular database connection if the connection fails, as you'll just generate more errors/exceptions if you try to use an invalid connection.
Reply
#7

But if I you intend to use the try / catch ... mistakes would also appear. I would not have solved the problem. or not?
Reply
#8

The intention is to do something in the catch block which is appropriate for your site, rather than allowing the error to be displayed to the user. You might choose to setup a boolean variable/property to tell the rest of the code not to use that particular database connection, or you might just return a value indicating to the calling function that the database wasn't available. The point is to prevent the site/page from going down just because the connection isn't available, but it's still up to you to make sure you don't try to use the connection.

In terms of how you handle the absence of the data that connection would have provided, that's also determined by what's appropriate for your site. You might choose to display a message where the data would have been displayed which says the data is not currently available, your you might not display anything.
Reply
#9

(This post was last modified: 11-13-2015, 12:22 PM by StratoKyke.)

I tried to do a test, but while entering the code and letting the empty catch so they do not give errors, and more. Errors appear equally.

There would be a way to change the driver, so make certain controls only the default group and others set them free? so do not give errors?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB