Jump to content

Check database name before running a query in phpMyAdmin


cyberRobot
Go to solution Solved by Barand,

Recommended Posts

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.

Link to comment
Share on other sites

  • Solution

I can think of only three  ways

 

  1. Only ever have a single database on your server
  2. Execute the statement "USE databasename" before every query.
  3. 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
  • Like 1
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.  :confused:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.