Skip to content

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

Closed

Description

@alditis

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?

Activity

  1. 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.[/+] on Jun 21, 2024
  2. SteveRedding commented on Jul 28, 2024

    @SteveRedding

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

  3. sidorares commented on Jul 28, 2024

    @sidorares
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions