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 have data that I want to pull from a mySQL database and I
I want to refactor some C# modules that read data from SQL Server 2008
I want to create a Java application bundle for Mac without using Mac. According
I want to fetch a branch from a remote, but my branch name does
I want to create a client side mail creator web page. I know the
I want to log onto Stack Overflow using OpenID, but I thought I'd set
I would like to return a DTO from my data layer which would also
Im a new Android programmer, and my question is this: I want to fetch
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my

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.