4

Is there are any ways to stop javascript query on mysql table and wait until next row is inserted?

For example using function:

var query = connection.query('SELECT * FROM table');

I want to read all rows in table, display them and wait until next row is inserted, read inserted row and wait until other is inserted and so on...

Basically it should read table which is updated every time other table gets update from PHP or other script (trigger events).

I can not find any information about cursor sliding in javascript mysql query maybe there are some articles about it?

8
  • So basically you want to JavaScript to know when a new row is inserted? Why not hook your functionality to the function that inserts the row? That way, you always know when a new row is placed. Commented Nov 19, 2012 at 12:37
  • Well I am reading table which is created by mysql triggers from another table updates. Basically I want to display real time updates. And they are created by PHP script Commented Nov 19, 2012 at 12:41
  • Then it would be better if you explain in your post that you aren't the only one using the db and want to get updates from it. Otherwise, everyone could assume that only your app is using it. Commented Nov 19, 2012 at 12:43
  • Explain what other apps are doing? Commented Nov 19, 2012 at 12:53
  • no, just state that there are other programs using it. Commented Nov 19, 2012 at 12:54

1 Answer 1

2

You could poll for the answer like so, api made up as I've never used the mysql library for node.

Row Count Interval
var connection = ..., lastCount;

funciton mysqlPoll() {
    var query = connection.query('SELECT count(field) as count FROM table');

    query.exec(function(result) {
        if (result.count != lastCount) {
            //Select all the rows here using offset to skip the already processes
            //process them
            lastCount = result.count;
        }
    });
}

setInterval(mysqlPoll, 500); //every 1/2 second

Better if you have a primary auto-inc key Interval

var connection = ...,
    lastCount = 0;

funciton mysqlPoll() {
    var query = connection.query('SELECT * FROM table WHERE primaryId > '+lastCount);

    query.exec(function(result) {
        if (result.count != lastCount) {
            //Select all the rows here using offset to skip the already processes
            //process them
            lastCount = lastResultsPrimaryId;
        }
    });
}

setInterval(mysqlPoll, 500); //every 1/2 second

Another option would be to open a socket, (net, http, etc), on node.js and ping it from the php script to trigger an update.

Hope that helps

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

2 Comments

Thanks for helping. I know that MySQL is not the best choice to use with js, maybe you know some databases which support logical connections (I have two SELECT`s in one SELECT) and also have for example tailable cursors and oplog events like MongoDB?
Have you looked at github.com/1602/jugglingdb it allows you to use any db underneath the api. Without knowing the data structure and what your attempting to do it would be very difficult for me to help determine the proper db

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.