Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6343723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:30:01+00:00 2026-05-24T20:30:01+00:00

My code is as follows: var mysql= require(‘mysql’); var client = mysql.createClient({ user: ‘root’,

  • 0

My code is as follows:

var mysql= require('mysql');

var client = mysql.createClient({
    user: 'root',
    password: 'root',
    host: 'localhost'
});

client.query('USE sample');

for(var k=0;k<=100;k++)
{

    var sql="SELECT `id` FROM `data` WHERE `id`= ?";

    console.log(k);

    client.query(sql,[k],function(err,results,field){

        console.log("DATABASE "+k);
        if(results.length == 0) {

            var sql2="INSERT INTO `data` (`id`) VALUES (?);";

            client.query(sql2,[k],function(err,results,field){

            });

        }
    });

}

client.end();

When I run this code , the results are as follows:

1
2
3
...
100
Database 101
Database 101
Database 101
...
Database 101

and it neither closes the database connection nor inserts any data.

I want to insert data in the loop. But before inserting the new record , it also needs to check whether it already exists or not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:30:02+00:00Added an answer on May 24, 2026 at 8:30 pm

    The closure for the callback function binds to the variable k by reference, not by value. This means that by the time you execute the callbacks, they all get the latest k value (101). In order to bind to the current k value at the time of closure creation, you need to add a new variable scope. Perhaps something like this:

    for(var k=0;k<=100;k++) {
        var sql="SELECT `id` FROM `data` WHERE `id`= ?";
        console.log(k);
        (function () {
            var kCopy = k;
            client.query(sql,[kCopy],function(err,results,field){
                console.log("DATABASE "+kCopy);
                if(results.length == 0) {
                    var sql2="INSERT INTO `data` (`id`) VALUES (?);";
                    client.query(sql2,[kCopy],function(err,results,field){});
                }
            });
        }());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What advantage, if any, is provided by formatting C code as follows: while(lock_file(lockdir)==0) {
So, simple procedure, calculate a factorial number. Code is as follows. int calcFactorial(int num)
I've written a rails app that follows the regular directory structure (model code in
Ok, here is the code and then the discussion follows: public class FlatArrayList {
I have a JSON as follows { columns : [RULE_ID,COUNTRY_CODE], RULE_ID : [1,2,3,7,9,101,102,103,104,105,106,4,5,100,30], COUNTRY_CODE
I'm trying to follow code-to-interface on a project. Should I be creating an interface
I was wondering what everyone thinks of this. Is the code easy to follow?
The Code Snippet is as follwos namespace RecruiterWebService { /// <summary> /// Summary description
How is your javaScript code organized? Does it follow patterns like MVC, or something
What guidelines do you follow to improve the general quality of your code? Many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.