简体   繁体   English

JavaScript for循环似乎没有改变'i'的值吗?

[英]Javascript for loop doesn't seem to be changing the value of 'i'?

I'm trying to run this loop in which I would like the value of 'i' to change. 我正在尝试运行此循环,在此循环中我想更改“ i”的值。

However, the value seems to be stuck at zero for all 4 iterations of the loop. 但是,对于循环的所有4次迭代,该值似乎都停留在零。

for(var i=0;i<5;i++){

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

Does anyone have any advice on why this is happening? 有谁对为什么会这样有任何建议吗? Thanks! 谢谢!

You didn't include the variable i in your query, you just queried for the hardcoded string ("++i++") . 您没有在查询中包含变量i ,只是查询了硬编码字符串("++i++")

This: 这个:

client.query('SELECT curattend FROM table1 WHERE ind=("++i++")', function(err,result){

should be: 应该:

client.query('SELECT curattend FROM table1 WHERE ind = ?', [i], function(err,result){

You've put "++i++" inside a string enclosed with single qoutes ('). 您已将“ ++ i ++”放在用单个qoutes(')括起来的字符串内。 You must use single quotes to escape the string as well. 您还必须使用单引号将字符串转义。

Use this: 用这个:

client.query('SELECT curattend FROM table1 WHERE ind=('+i+')', function(err,result){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM