1

This might be a silly question. I have two separate databases that are being used with Laravel 4. One of them can only be accessed with a certain IP (security reasons) while the other can be accessed. I have two different mysql connections. I have seen the Database connection test using this:

if(DB::connection('mysql')->getDatabaseName()){ }

To test what can be seen and what can't be seen, I tried to give the mysql a false password. I get this nice ugly error how it can't connect. Is there a way to make it where if the Database cannot be reached, just to ignore it? There's only one PHP class that's calling the secure only database on page load, but the above check doesn't seem to be working.

2
  • can you reach your remote sql server with just plain php ? have you configured the remote sql server to accept connections ? if you're running xampp you might wanna read this stackoverflow.com/questions/8481620/… Commented Jan 22, 2015 at 3:40
  • @astro, I'm able to connect just fine while on a certain IP and this is exactly what I wanted to do, but I wanted some code NOT to run while outside of the IP. :) Commented Jan 22, 2015 at 16:33

1 Answer 1

2

Going through the core code of laravel, there is no specific exceptions being thrown when a database connection fails.

The solution hence, is:

try {
    //Strings always evaluate to boolean true
    $dbConnected = (bool)DB::connection('mysql')->getDatabaseName();
}
catch (Exception $e)
{
    $dbConnected = false;
}

Then work your code based on the variable $dbConnected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.