PHP & MySQL Setting up database MySQL Workbench ERROR CODE 1046

Hello there. I’m following this book through for the third time (I’m a slow learner)., and have run into a little error.

I’m around page 99, where Kevin describes setting up a new schema (ijdb), and then a new table (joke), and then inserting some entries:

INSERT INTO joke
(joketext, jokedate) VALUES (
"A programmer was found dead in the shower. The instructions read: lather, rinse, repeat.",
"2017-06-01")

this produces an error for me Error Code: 1046. No database selected

I’ve done some searching in these forums and if I do the following

USE ijdb;
INSERT INTO joke
(joketext, jokedate) VALUES (
"A programmer was found dead in the shower. The instructions read: lather, rinse, repeat.",
"2017-06-01") 

or

INSERT INTO ijdb.joke
(joketext, jokedate) VALUES (
"A programmer was found dead in the shower. The instructions read: lather, rinse, repeat.",
"2017-06-01") 

Then both work.

So I’m wondering firstly why it is necessary for me to explicitly select the database but it wasn’t for Kevin?
And secondly, will I have to follow this procedure when connecting via PHP later in the book?

I don’t know msqWorkbench as I have deleted it on my Mac because it crashes all the time :slight_smile: but normally you can “preselect” to which database the query should be attached by choosing the database in the databases list view and then choose “query” next to it.

For the PHP connection you can select the database directly when you create the connection. In that case you do not need to add the database name to the query. If you do not set a database in the connection string you have to add the db name as well.