INSERT command error 'no database selected'

I’m using the book PHP & MySQL: Novice to Ninja, 6th Edition. I’ve created a database as described in Chapter 3. Bit when I try to run the script
INSERT INTO joke
(joketext, jokedate) VALUES (
“A programmer was found dead in the shower. The instructions read: lather, rinse, repeat.”,
“2017-06-01”)
it produces an error message that no database is selected. However, it is!
Any help greatly appreciated.

I suspect on this occasion the computer is right! :biggrin:
You may think you’ve connected to the database, but not done so (or not done so correctly).

However, to be sure what is wrong, we will need to see your code. You can post it in your reply.

When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

You may be tripping yourself up on your teminology.

You’ve connected to the database server, but the engine is telling you you’ve not told the server what database you want to USE.

Most likely you’ve either forgotten part of, or typoed, your DSN when you connected to the server. So specifically, i’d be looking for that part of your code. (censor out your password though!)

run this sql statement before you run your query –

USE databasename;

substitute the name of the database where you created your table

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

Code as requested. Thank you so much for replying.

Thank you so much for your reply. However, I’ve copied the configuration of the ‘Homestead’ server as per the book I’m working through. Testing connection works fine. Is that what you meant?

Thank you very much for your reply. I tried that bit it didn’t work. I get an error 1064: You have an error in your SQL syntax.

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

A bit more information that may be useful. This command works fine:

SELECT * FROM ijdb.joke;

well, it sure looks like ijdb is your database name, not joke as you have in post #7

could you please try the USE statement again, and don’t forget the semicolon

Thank you. I now entered:

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")

I now get an error message code 1406. Data too long for column ‘joketext’

Hope that makes sense.

it sure does

what do you suppose could be causing this error?

That’s not in the book! I didn’t set any length. It’s not mentioned. Just varchar(45) - whatever that is. Maybe number of characters? But how to change?

I reduced the number of characters and it worked fine. So thank you for that. Is there any way of increasing the number of characters?

you did set the length LOL

easiest way is just to drop your table and re-create it with a larger varchar column value

alternatively you could do ALTER TABLE CHANGE COLUMN

Why did you set joketext to VARCHAR(45)? I can’t see where in the book it suggests that. From what I can find it suggests using TEXT.

Same i am also getting aIso get an error 1064, i have to check something to remove this error ?

yes, check this thread, and check your column definitions

Thanks heaps. Really appreciated.

My error! But just couldn’t see it. Thank you!

Thank You for your concern