Skip to content
Advertisement

Query to check if MySQL database is connected

I am wondering if there is a best practice or standard query used to check if a MySQL database is still connected?

I was originally thinking of performing a simple search in one of my tables, which will confirm if the database is still there. Just wondering if there is a better approach.

Advertisement

Answer

No need to actually select from a table. You could just do:

select 1

This is a very cheap query, that will error if the database is not connected. The 1 does not really matter (the important point is to not have a from clause). You could as well use select null, or select 'foo'.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement