How does a MySQL query work with and without placeholders (?)?

I have two approaches for executing SQL query in node.js , I want to know the difference between the two. Which one is recommended ? and why ??

First approach

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

Second approach

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

Please tell me the exact use of these two mechanisms.

+5
source share
1 answer

Double Question Syntax ?? in the substitution line wraps replaceable parameters in delimiters before sending them to the server. Separators are suitable for table and column names. So your first example, 'SELECT ?? FROM ?? WHERE id = ?' 'SELECT ?? FROM ?? WHERE id = ?' sends

 SELECT `username`, `email` FROM `users` WHERE id = '1' 

to the server.

Second, 'SELECT username,email FROM UserTable WHERE id=?' sends

 SELECT username,email FROM UserTable WHERE id='1' 
+4
source

Source: https://habr.com/ru/post/1262694/


All Articles