0

I'm currently working with JS, DiscordJS and MySQL and trying to get some data from the table which usually always worked fine. But I'm struggling with a MySQL SELECT query because it doesn't checks if the value in the table is exectly the same as it is searching for. The end of the Discord ID is different then the one it's searching for.

Code:

let query = `SELECT * FROM accounts WHERE DiscordID = ${interaction.member.id}`;
console.log(`QUERY: ` + query);
MySQL.Connect()
    .then((connection) => {
        MySQL.Query(connection, query)
            .then((results) => {
                if (results == '') {
                    console.log('[RESULTS = 0]');
                    console.log(results);
                } else {
                    console.log('[RESULTS > 0]');
                    console.log(results);
                }
            })
            .catch((error) => {
                console.log(error);
            })
            .finally(() => {
                connection.end();
            });
    })
    .catch((error) => {
        console.log(error);
    });

Result:

QUERY: SELECT * FROM accounts WHERE DiscordID = 466934561738326016

RESULT: 
ID: 1,
Username: 'Test',
Password: 'Test',
DiscordID: '466934561738326022'

MySQL schema:

CREATE TABLE IF NOT EXISTS `accounts` (
`ID` INT NOT NULL AUTO_INCREMENT, 
`Username` VARCHAR(255) NOT NULL, 
`Password` VARCHAR(255) NOT NULL, 
`DiscordID` VARCHAR(100) NOT NULL, 
PRIMARY KEY (`ID`));
O. Jones
  • 103,626
  • 17
  • 118
  • 172
TheDivine
  • 13
  • 1

1 Answers1

0

You were bitten by MySQL's handling of numbers.

You said,

SELECT * FROM accounts WHERE DiscordID = 466934561738326016

MySQL took that long number and silently converted it to an IEEE 754 64-bit floating point number, losing precision. Doing SELECT 466934561738326016; gets you 466934561738326000. That matches the wrong DiscordID, sometimes. !!

If you put quotes around the ID, doing this instead, MySQL leaves the ID value intact as a text string. It works.

SELECT * FROM accounts WHERE DiscordID = '466934561738326016'

Check it out here. fiddle

O. Jones
  • 103,626
  • 17
  • 118
  • 172