在MYSQL查询中使用Javascript变量。

时间:2022-03-31 21:11:35

I've been working on this problem for a while and can't seem to find any good info on it.

我做这个问题已经有一段时间了,似乎找不到任何好的信息。

I'm running a node.js server on an EC2 instance and need to add rows to a MYSQL table with the following code:

我运行一个节点。在EC2实例上的js服务器,需要将行添加到一个MYSQL表中,其代码如下:

client.query('SELECT curattend FROM table1 WHERE ind=1', function(err,result){
        att = result[0].curattend;
        console.log(att);

        client.query("INSERT INTO archive (attendance) VALUES ('att')", function(err,info){
                });

        console.log(att);

        });

I printed 'att' before and after just to be sure...att is equal to '233'. However, the number'0' keeps getting uploaded into the MYSQL table.

我在之前和之后都打印了“att”,以确保……att等于233。然而,数字“0”一直被上传到MYSQL表中。

Can anyone point me to a resource that can help me solve this?

有人能告诉我一个能帮我解决这个问题的资源吗?

2 个解决方案

#1


3  

Based on user2246674's constructive comments, I also learned something today.

基于user2246674的建设性意见,我今天也学到了一些东西。

Rather than this:

而不是:

client.query("INSERT INTO archive (attendance) VALUES (" + att + ")"

Try this instead:

试试这个:

 var att  = result[0].curattend;
 client.query("INSERT INTO archive (attendance) VALUES (?);", [att], function(err,info){ });
 // This creates the insert statement INSERT INTO archive (attendance) VALUES (att);

#2


0  

Your code should be:

您的代码应该是:

('"+att+"')"

#1


3  

Based on user2246674's constructive comments, I also learned something today.

基于user2246674的建设性意见,我今天也学到了一些东西。

Rather than this:

而不是:

client.query("INSERT INTO archive (attendance) VALUES (" + att + ")"

Try this instead:

试试这个:

 var att  = result[0].curattend;
 client.query("INSERT INTO archive (attendance) VALUES (?);", [att], function(err,info){ });
 // This creates the insert statement INSERT INTO archive (attendance) VALUES (att);

#2


0  

Your code should be:

您的代码应该是:

('"+att+"')"