MySQL/NodeJS problem

Sup, so first, I want to achieve a simple thing, select a database using the “USE” command and then check if the database is selected with “SELECT DATABASE() FROM DUAL;” command.

The issue is that it does not select, and when I try to execute any query related to the database it says “400, No database selected”

Here is the output: (Ignore that “undefined” after the OkPacket since it’s because USE only returns 1 argument)

See that “null”, it’s because no database is selected.

Until now, I did not find any solution.

Interesting, have you succeed to define the database yet?

1 Like

Yes, the database is fully operational, works fine with phpMyAdmin and also works fine using the console itself to execute the commands that are shown in the script

That’s really weird Imma look up what’s wrong can you send the connection script?

1 Like

The connection itself is the mysql Pool, outside from that there is nothing, I mean nothing uses the connection except the one I sent. And Session is just a class, if you want I can explain what it is.
image

That is when the connection is created, those values are the normal ones, port is the default, and the other ones are credentials.

I forgot to mention, MySQL is the library “mysql” from npm

image
This is me trying those commands in console

Wait wait I got something, at least something different, so I did it so as soon as the connection is created it tries to use the database,
image

The other use still returns null tho, idk why and the error still happens


Its the same exact code, idk why the other one dosent work tho

Wait wait, the newSession.connection doesn’t work (newSession being the same as the “Session” you saw in the first pic)
image

I think I know what’s the problem, I will later tell if it works or not

Code:

YESSS, the solution.

So really important, the “mysql.createPool” function returns a pool, normally you would use this pool for doing the query, since well it looks the same as the other one I am going to mention BUT THEY ARE NOT, the connection you actually want to use is the one that is given to you after you create the Pool, this one:
image

Using the normal pool won’t work even tho it looks like it should.

I dont think you need the ; in the database name

I mean you don’t need it, but I like to add it since it doesn’t change anything. But yes I did test to see if that was the problem, it was not