Skip to content
Advertisement

No database selected error message

I am changing all my queries that are using PHP MySQL to MySQLi.

I have made a file called db.php with the connection settings.

The file includes

JavaScript

I include the file with:

JavaScript

after that i use:

JavaScript

It echo’s Connected so I assume my connection is good.

I have 3 PHP variables which I want to insert in my database table Code

I first echo the variables so I am sure they have content.

After I validated my connection is alright (returned Connected) and echoing the content of the variables I want to do the query with:

JavaScript

I get the message Errorcreating : No database selected

I have the echo $sql to show me the query.

If I copy the query directly in SQL it works like it should.

This is my first time on MySQLi so it’s possible I made a very dumb mistake but I can’t find it.

Advertisement

Answer

When opening the connection you can pass the database name as a 4th parameter:

JavaScript

Also, your escape character is wrong. Don’t use single quotes around table names, use backtick operator instead

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