Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql参数周围是否需要括号来防止sql注入?_Mysql_Node.js - Fatal编程技术网

Mysql参数周围是否需要括号来防止sql注入?

Mysql参数周围是否需要括号来防止sql注入?,mysql,node.js,Mysql,Node.js,我使用的是nodejs和mysql npm包,我试图从一个表中进行选择,其中其他_文本= 下面是它的外观: var query = connection.query(`SELECT id FROM ${tableName} WHERE other_text = ?`, attributeName.other_text, function (err, rows) { ... 我用什么读过?将自动转义用户输入的字符串。在我看到的大多数这样做的示例中,查询函数中的第二个参数周围都有括

我使用的是nodejs和mysql npm包,我试图从一个表中进行选择,其中其他_文本=

下面是它的外观:

var query = connection.query(`SELECT id FROM ${tableName} WHERE other_text = ?`,
    attributeName.other_text, function (err, rows) {
    ...
我用什么读过?将自动转义用户输入的字符串。在我看到的大多数这样做的示例中,查询函数中的第二个参数周围都有括号,如下所示:

var query = connection.query(`SELECT id FROM ${tableName} WHERE other_text = ?`,
    [attributeName.other_text], function (err, rows) {
    ...
为了转义传入的字符串,是否需要括号?当我尝试它时,它可以工作,但我甚至不知道如何测试SQL注入,所以我真的不知道括号是否必要,甚至是否正确


谢谢。

括号代表一个数组。如果要在查询中使用更多值,可以使用数组

例如,假设您要从表中选择多个列,并将它们传递给语句,您可以使用如下方式:

connection.query(`SELECT ?? FROM ${tableName}`,
[col1, col2, col3], function (err, rows) {
const tableName = 'users';
const whereCondition = {id: 1};
const whaToUpdate = {name: 'newName'}
const mysql = require('mysql');
const statement = mysql.format('update ?? set ? where ?', [tableName, whaToUpdate , whereCondition]);
connection.query(statement, (error, result, fields) => { });
它也可以与字符串、数字甚至对象结合使用。假设您想从Users表更新id为1的用户。您可以这样做:

connection.query(`SELECT ?? FROM ${tableName}`,
[col1, col2, col3], function (err, rows) {
const tableName = 'users';
const whereCondition = {id: 1};
const whaToUpdate = {name: 'newName'}
const mysql = require('mysql');
const statement = mysql.format('update ?? set ? where ?', [tableName, whaToUpdate , whereCondition]);
connection.query(statement, (error, result, fields) => { });
为了更好地阅读代码,我还建议使用.format。 最后你会有这样的东西:

connection.query(`SELECT ?? FROM ${tableName}`,
[col1, col2, col3], function (err, rows) {
const tableName = 'users';
const whereCondition = {id: 1};
const whaToUpdate = {name: 'newName'}
const mysql = require('mysql');
const statement = mysql.format('update ?? set ? where ?', [tableName, whaToUpdate , whereCondition]);
connection.query(statement, (error, result, fields) => { });

它在没有支架的情况下工作吗?@SergioTulentsev是的。我只是担心如果没有括号,它不会逃逸数据。