Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

namedPlaceholders: "ER_WRONG_ARGUMENTS" in data object with property number type for LIMIT clause. #2793

Closed
alditis opened this issue Jun 21, 2024 · 2 comments

Comments

@alditis
Copy link

alditis commented Jun 21, 2024

Description

I have noticed some behavior that I did not expect in the LIMIT clause.
When I run the query with data.LIMIT = 3 (number type) not works it.

const data = {
    USER: "myuser", // Ok
    COUNT: 10,      // Ok
    LIMIT: 3        // Error
};

But with data.LIMIT = "3" (string type) works it.

const data = {
    USER: "myuser", // Ok
    COUNT: 10,      // Ok
    LIMIT: "3"      // Ok
};

Reproducing error

import mysql from 'mysql2/promise';

const connection = await mysql.createConnection({
    host: 'myhost',
    user: 'mydbuser',
    password: 'mydbpass',
    database: 'mydb',
    namedPlaceholders: true
});

(async()=> {
    const sql = "SELECT `code` " + 
            "FROM `tag` " +
            "WHERE `author` = :USER AND `count` = :COUNT " + 
            "LIMIT :LIMIT";

    const data = {
        USER: "myuser", // Ok 
        COUNT: 10,      // Ok
        LIMIT: 3        // With number type not works it, but with string type "3" works it.
    };
    
    try {
        const [results] = await connection.execute(sql, data);
        console.log(results);
    } catch (err) {
        console.log(err);
    }
})();

The error

Error: Incorrect arguments to mysqld_stmt_execute
    at PromiseConnection.execute (..AbortController../src/node_modules/mysql2/promise.js:112:22)
    at file://.../src/test.js:31:44
    at file://.../src/test.js:36:3
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ER_WRONG_ARGUMENTS',
  errno: 1210,
  sql: 'SELECT `code` FROM `tag` WHERE `author` = ? and `count` = ? LIMIT ?',
  sqlState: 'HY000',
  sqlMessage: 'Incorrect arguments to mysqld_stmt_execute'

Environment

mysql2: 3.10.1
node: v20.13.1

Question

Is there a way to avoid the error and use the number type in LIMIT clause without having to convert it to a string type?

@alditis alditis changed the title namedPlaceholders: "ER_WRONG_ARGUMENTS" in data object with property number type. namedPlaceholders: "ER_WRONG_ARGUMENTS" in data object with property number type for LIMIT clause. Jun 21, 2024
@SteveRedding
Copy link

#1239 in prepare statement mysql2 may convert javascript number type into
mysql double type.

@sidorares
Copy link
Owner

Is there a way to avoid the error and use the number type in LIMIT clause without having to convert it to a string type?

not at the moment, but hopefully soon should be possible with api similar to #1239 (comment)

I'll close this issue for now, feel free to ask questions here @alditis but the progress of the root cause is tracked by #1239

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants