2

I am wondering how I would get data from a MySQL database and display it in real time using PHP. Without having to refresh the page. Thanks!

1

3 Answers 3

5

Use AJAX (I suggest using the jQuery library for this), and have your AJAX script (written in PHP) query the MySQL database.

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

Comments

2

You can use Socket.Io for more Speed ,efficiency and performance for Your Server

http://socket.io/

But you need Node.Js for that

Comments

1

You will have to use javascript. You can use setInterval(function, n) to fire your update calls every n milliseconds, and a library like jQuery to handle the ajax call and updating your page.

Download jQuery or link to a CDN hosted version of jQuery in your page. Then put something like this on your page:

setInterval(function(){
    // inside here, set any data that you need to send to the server
    var some_serialized_data = jQuery('form.my_form').serialize();

    // then fire off an ajax call
    jQuery.ajax({
        url: '/yourPhpScriptForUpdatingData.php',
        success: function(response){
            // put some javascript here to do something with the 
            // data that is returned with a successful ajax response,
            // as available in the 'response' param available, 
            // inside this function, for example:
            $('#my_html_element').html(response);
        },
        data: some_serialized_data
    });
}, 1000); 
// the '1000' above is the number of milliseconds to wait before running 
// the callback again: thus this script will call your server and update 
// the page every second.

Read the jquery docs under 'ajax' to understand the jQuery.ajax() call, and read about 'selection' and 'manipulation' if you don't understand how to update the html page with the results from your ajax call.

The other way to continuously update your page is to use a persistent connection, like web sockets (not currently supported across all the common browser platforms) or a comet-style server-push setup. Try googling comet and/or web sockets for more info, but I think the above method is going to be much easier to implement.

1 Comment

this causes one mysql connection per second. Closing the connections does not seem to work. How do we prevent maxing out the connections?

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.