cyberRobot Posted August 5, 2016 Share Posted August 5, 2016 When running a query in phpMyAdmin, is there a way to make sure the correct database is selected before executing the query? I know that the following code lets you see which database is selected: SELECT DATABASE(); But I'm not sure how to use the information to prevent queries from running when the wrong database is selected. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 5, 2016 Solution Share Posted August 5, 2016 I can think of only three ways Only ever have a single database on your server Execute the statement "USE databasename" before every query. In INSERT, UPDATE and DELETE queries always prefix the table names with dbname (ie dbname.tablename). No damage is done with SELECT queries I suppose a fourth way is be careful what you are doing 1 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 5, 2016 Author Share Posted August 5, 2016 Huh...option 3 actually works! It doesn't seem to matter which database I'm in when clicking the SQL tab and running the query. And the technique even works at the "localhost" level. For what it's worth, the "USE databasename" option didn't work. If you're in the wrong database, an INSERT query, for example, still works if that database happens to have a table with the same name used in the query. I was hoping the phpMyAdmin would at least flag the error. Thanks Barand! Quote Link to comment Share on other sites More sharing options...
Barand Posted August 5, 2016 Share Posted August 5, 2016 For what it's worth, the "USE databasename" option didn't work. If you're in the wrong database, an INSERT query, for example, still works if that database happens to have a table with the same name used in the query. I was hoping the phpMyAdmin would at least flag the error. The idea is to "USE databasename" to set the correct database name as default Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 5, 2016 Author Share Posted August 5, 2016 The idea is to "USE databasename" to set the correct database name as default That works when running the queries at the "localhost" level. However, it doesn't seem to do anything when running queries at the database level in phpMyAdmin. It doesn't flag an error. It just seems to ignore that statement altogether. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.