20

Am sure this is fairly obvious, to me it would make more sense if the second double question mark in the example was a single one.

From their docs:

Alternatively, you can use ?? characters as placeholders for identifiers you would like to have escaped like this:

var userId = 1;
var columns = ['username', 'email'];
var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function(err, results) {
  // ...
});

console.log(query.sql); // SELECT `username`, `email` FROM `users` WHERE id = 1
Sean McClory
  • 4,195
  • 3
  • 32
  • 35
  • 2
    I don't know node-mysql, but looking at your example statement, it looks like `??` is used for identifiers (table names, column names), whereas `?` is used for values (strings, numbers) – knittl May 26 '15 at 11:24

2 Answers2

35

?? is used for table and column names, it escapes them with backticks. ? is for ordinary values.

Barmar
  • 741,623
  • 53
  • 500
  • 612
4

Barmar is right but here I am giving an example.

?? is used for table names and column names ? is used for normal values like parameter value in where clause.

let insertQuery = 'INSERT INTO ?? (??,??) VALUES (?,?)';
let query = mysql.format(insertQuery,["todo","user","notes",data.user,data.value]);
pool.query(query,(err, response) => {
    if(err) {
        console.error(err);
        return;
    }
    // rows added
    console.log(response.insertId);
});

In the above example, first three ?? mark take table name and column name (todo is the table name and user, notes are the column name). Last two ? take normal values those will be plain data used to be inserted into todo table.

Khabir
  • 5,370
  • 1
  • 21
  • 33