Site online with db offline |
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?
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.
I deactivate the db_debug, I turn off the database and I received this error:
Code: A PHP Error was encountered 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 The connections I make them by the manual: $DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE);
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
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.
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.
But if I you intend to use the try / catch ... mistakes would also appear. I would not have solved the problem. or not?
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.
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? |
Welcome Guest, Not a member yet? Register Sign In |