在查询中附加单引号('')

时间:2013-11-20 13:03:47

标签: javascript mysql node.js

我对mysql查询有一个小问题。 (通过node.js发送)

res.send(SELECT `id` FROM table where `id` ='+id)

给出id = 4的结果

select id from table where id=4

但我需要在查询中插入单引号。

4 个答案:

答案 0 :(得分:2)

以下是您的需求:

res.send("SELECT id FROM table where id ='"+id+"'")

请注意,这会让您感受到SQL injections。请阅读node-mysql's documentation,了解如何避免此安全风险。

答案 1 :(得分:0)

双引它:

'SELECT id FROM table where id ='''+id+''''

e.g。

SELECT * FROM [table] WHERE column LIKE '%te''st%'

将选择列值包含te'st

的所有行

答案 2 :(得分:0)

res.send("SELECT id FROM table where id ='"+id);

答案 3 :(得分:0)

假设需要引用id值:

res.send("SELECT id FROM table where id ='"+id + "'")

我个人对来自Javascript的直接SQL查询持怀疑态度 - 这通常意味着您的应用程序容易受到淘气的人的攻击。<​​/ p>