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 3698490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:59:14+00:00 2026-05-19T04:59:14+00:00

I want to fetch some data from mySQL without refreshing the page using jQuery.

  • 0

I want to fetch some data from mySQL without refreshing the page using jQuery. Can someone please tell me how I can fetch the data if any record in mysql table is updated. For example, following code fetches a number count from the database, if someone add a number in the database and it is updated, how I can display the new number without page refresh? Thanks.

<?
$query  = "SELECT number, name FROM members WHERE id=1";
$result = mysql_query($query);

$row = mysql_fetch_array($result);
$number = $row['number'];    ?>
  • 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-19T04:59:15+00:00Added an answer on May 19, 2026 at 4:59 am

    It’s been mentioned two times already, but I still want to underline it. You would never want a client to be able to access the database directly. It is a serious security risk.

    Now to the solution. First of all you will want to set up a PHP-file that you can request with ajax. Let’s call this check.php and it will look something like this:

    <?php
    // include necessary files to enable connection to the database
    $query  = "SELECT number, name FROM members WHERE id=1";
    $result = mysql_query($query);
    
    $row = mysql_fetch_array($result);
    $number = $row['number'];
    
    // send correct content type header of json
    header("Content-Type", "application/json");
    
    // we create an array, and encode it to json
    // then echo it out while killing the script
    die(json_encode(array('numbers'=>$number)));
    

    And now onto the JavaScript. The solution is similar to that of Kyle Humfeld, but will not be using the setInterval, because it’s a really bad practice. The reason behind this is because setInterval will not care about the status of your ajax call, if it has finished or not. So you might end up with stacking requests if there’s something wrong with the server, and this is not good.

    So to prevent this, we instead use a combination of the success-callback of the ajax method (.getJSON is essentially a shorthand for .ajax) and setTimeout to create something that’s called polling:

    $(function(){
        // placeholder for request
        var request = null;
    
        // request function
        var check = function() {
            request = $.getJSON('check.php', function(data){
                // this will log the numbers variable to the dev console
                console.log(data.numbers);
                // initiate a timeout so the same request is done again in 5 seconds
                setTimeout(check, 5000);
            });
        });
    
        // initiate the request loop
        check();
    
        // if you want to cancel the request, just do request.abort();
    });
    

    Also, there’s a more advanced solution using a comet server that will push data from the server to the client, but you should try to get the above to work first before digging into comet. If you want to read up on the subject I suggest that you take a look at APE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some issues returning data from on page, using jQuery, PHP and
I want to be able to store (not echo) some data that has been
I've been looking into some mobile development frameworks lately. The app I want to
I want some advice on structuring some (basic) php code. I need to display
I am making use of jQuery's .autocomplete plugin, and I would appreciate some assistance
I'm very new at using JS and AJAX. Currently I'm working with Google Maps
First let's explain what I want to do and then ask my question! Well,
I'm currently building a custom photoalbum but now I'm stuck with a mysql query.
I have a while() function that loops a row of data within my table.

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.