skip to Main Content

I want to get multiple user records from MariaDB:

const mariadb = require('mariadb');
const pool = mariadb.createPool(...);
const conn = await pool.getConnection();
const query = `SELECT * FROM user WHERE id IN (?)`;
const rows = await conn.query(query, [requestTargets]);

where requestTargets is a string of ids like ‘123,124,125’

The problem is that this results in array-type ‘rows’ with single element – the user with id which is the first in requestTargets.

I’ve tried making the same request from phpmyadmin and it returned valid result of multiple records.

I’ve tried to pass requestTargets in different format to query (as an array of integers or strings) but this resulted in SQL errors.

What can be wrong?

2

Answers


  1. Try sending an array from an array as a parameter.

    Example:

    const mariadb = require('mariadb');
    const pool = mariadb.createPool(...);
    const conn = await pool.getConnection();
    const query = `SELECT * FROM user WHERE id IN (?)`;
    const rows = await conn.query(query, [[requestTargets]]);
    
    Login or Signup to reply.
  2. Binding a list into an IN() clause is probably not handled in any Connector.

    If id is numeric, then you need to end up with IN (123,124); that is no quotes.

    If it is a string, then you need lots of strings: IN ('ab', 'cde')

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search