How to fix the “#1046: No database selected” error in phpMyAdmin

Here is a simple solution to the “#1046: No database selected” error when trying to import a .sql file in phpMyAdmin.

Solution #1: Before importing

In phpMyAdmin, click on the Export tab from the home page of phpMyAdmin, without selecting any database. phpMyAdmin will then include instructions to create and select the database when you import it.

This solution does not apply to most shared hosts, since they won’t allow you to create a database from phpMyAdmin.

Solution #2: The alternative fix

Unlike solution #1, this one also works on shared hosts.

  • Create the new database on your server, then write down the name.
  • Open your .sql file with a text editor
  • Insert the following line right before the first CREATE TABLE instruction in your .sql file: USE your_database_name_here;
  • Save your file, then import it in phpMyAdmin.

22 comments on “How to fix the “#1046: No database selected” error in phpMyAdmin

  1. Just wanted to add, since your solution led me to the answer :
    It’s all because there is an existing database of the hosting company and they won’t allow you to open one yourself, but rather suit yours into theirs. The solution therefore is to
    1.export your database in sql format from phpmyadmin localhost
    2.open that file with notepad++
    3.edit the first lines to this
    CREATE DATABASE IF NOT EXISTS hosting_db01 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
    USE hosting_db01;
    where ‘hosting_db01′ is the name of the hosting database, instead of your database
    4.spread the luuuv :)
    Good luck – worked for me and thanx again

  2. thanks, this was super helpful. found a junk stackoverflow post with like 2 dozen replies. and then with simplicity and good form your post workt. Did think you could add a little more of an explanation as to why for noobs but you just do you homie.

  3. this what i get when i insert the code create database tracking;use database tracking;

    error

    1044 – Access denied for user ‘cpses_liJMNZ2Gz8′@’localhost’ to database ‘tracking’

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax