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

  • SEARCH
  • Home
  • 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 8775997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:56:39+00:00 2026-06-13T18:56:39+00:00

Consider the following code: function dbTask(q) { mysql = new MySQL(); mysql.host = sqlHost;

  • 0

Consider the following code:

function dbTask(q) {
    mysql = new MySQL();
    mysql.host = sqlHost;
    mysql.user = sqlUser;
    mysql.password = sqlPassword;
    mysql.query("USE stock");

    return mysql.query(q, function(err, results, fields) {
        if(err) {
            console.log("MySQL Error: " + err + ", Query: " + q);
            return false;
        } else {
            return results; //here
        }
    });
};

var r = dbTask("SELECT * FROM user;");
console.log(r);

Whereas, I want the results to be returned from the inner anonymous function when calling dbTask() in the second last line, I am getting different output which seems like some internal construct of the mysql library under use.

How can I get dbTask to return the results when called?

  • 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-06-13T18:56:40+00:00Added an answer on June 13, 2026 at 6:56 pm

    Since mysql.query is asynchronous, then you’ll have to re-think your architecture.

    Instead of having your function return true or false, you’ll have to pass in handlers to be called if the query returned true or false.

    Something like this:

    function dbTask(q, success, failure) {
        mysql = new MySQL();
        mysql.host = sqlHost;
        mysql.user = sqlUser;
        mysql.password = sqlPassword;
        mysql.query("USE stock");
    
        mysql.query(q, function(err, results, fields) {
            if(err) {
                console.log("MySQL Error: " + err + ", Query: " + q);
                failure(err, results, fields);
            } else {
                success(results, fields);
            }
        });
    };
    

    Which you’d call like this:

    dbTask(q, 
              function(results, fields) { /* ... */ }, 
              function(err, results, fields) { /* ... */ });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: function getUser($uid) { global $_memcache; $u = $_memcache->get(user-.$uid); if( !$u)
Consider the following segment of code: function loadSomeContent() { URLLoader loader = new URLLoader(http://www.somesite.com/);
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
Consider the following code: $(window).unload(function () { console.log('foo'); }); If I trigger the unload
Let's consider the following scenario: a function which can generate code colors from white
Consider the following snippet of code: function parseXml(xml) { xmlObject= xml; alert(xmlObject.xml); } function
Consider the following code: $('div').click(function(){ $(this).animate({height:100}, 500) $(this).css({opacity:1}); }); Versus: $('div').click(function(){ $(this).animate({height:100}, 500); })
Please consider the two following snippets of code: (function f() { var x; try
Consider the following code: var Widget = new Class({ Implements: [Options], options: { name

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.