0

I have been trying to find a way to use the MySQL row count as a variable in Javascript. After fooling around for a while, I always get 'undefined' as my variable.

I'm very new to PHP and MySQL, but I somewhat know my way around Javascript.

So far I've tried the following:

script.js

var result = (function() {
            $.get( "submissioncount.php", function( data ) {
              console.log( data );
              return data;
            });
        })();

submissioncount.php

$sql = "SELECT * FROM table";

mysql_select_db('database');
$result = mysql_query( $sql, $conn );

$num_rows = mysql_num_rows($result);

echo "$num_rows";

The console.log is giving me the proper numerical value, however, 'result' is undefined.

Any tips as to how to get this working?

Thanks.

3
  • Where is your $conn variable? Commented Apr 23, 2014 at 23:35
  • Probably in a config file. It's outside the scope of the question. But please switch to mysqli functions before they are removed. They are already deprecated. Commented Apr 23, 2014 at 23:38
  • mysql functions have been deprecated? Oh man. Anything I should look out for when converting mysql functions to mysqli? Commented Apr 24, 2014 at 0:00

2 Answers 2

1

You need to create a callback inside of the $.get or use promises to set your variable. It could also be that you are returning from a nested function. Please read about defered.

Sign up to request clarification or add additional context in comments.

1 Comment

fwiw a promise will only let you chain the callback more cleanly, it will not let you OP set the variable the way they are trying here.
0

You're having a problem with scoping that can be tricky to catch when you're new to asynchronous programming - which all AJAX calls by default are.

When you $.get something, jQuery makes an http call to your server, which at some point responds with an answer. Since your browser doesn't know how long that response will take, it lets that call wait in the background until the server has replied, instead of blocking the rest of the site from functioning. When your browser gets the response, it can perform a function that you gave it ahead of time, with the information it got from the server - a callback.

The code you need looks like this:

function doThingsWithAjaxInfo (stuff) {
    // whatever you want your code to do with the information from the server
}

(function() {
        $.get( "submissioncount.php", function( data ) {
              doThingsWithAjaxInfo(data);
        });
})();

// the same thing but a little cleaner, your callback will automatically be called
// on the data that is returned from the server.

(function() {
        $.get( "submissioncount.php", doThingsWithAjaxInfo);
})();

Notice how there's no return in the get function. Since the ajax request finishes almost immediately (but does not fire its callback until the server replies), its return would not be useful for you (unless you want to use promises, but I don't recommend getting into them until you're more comfortable with a regular callback driven asynchronous control flow.

Handling scope in javascript is hard, especially when you use asynchronous techniques like ajax, but it makes your code incredibly powerful, and it's well worth the effort to get it right. Good luck!

3 Comments

the result is undefined because making an async call breaks the call stack. OP would have the same problem if the code used window.setTimeout instead of $.get, or any other asynchronous call.
Thanks a lot! I suppose I need to wrap my head around asynchronous programming. I have never really used it before now. Anything else I should look out for when programming like this?
The BIG UGLY PROBLEM with this kind of thing is usually nested callbacks - say you wanted to check the server for some info after you had the first response, you would have to do the same thing another level deeper, and so on. A friend of mine wrote a great blog post here about it. The number one thing is to not give up and keep trying, async programming is always going to be a little harder but it becomes totally manageable with practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.