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

  • Home
  • SEARCH
  • 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
Can someone please tell me how to invoke a method from the codeigniter controller
i get some data from mysql. $result = $forum_model->get_info($_SESSION['group_id']); // step 1 while($user =
I have a PHP server script that SELECTs some data from a MySQL database.
I have some data, yes, data. This data came from a MySQL query and
For some reason, JavaScript/PHP wont delete my data from MySQL! Here is the rundown
I'm trying to load some data via. jquery $get_desc = SELECT tekst_id,overskrift FROM rr_tekster
I have a PHP page and I want to share some data between pages
I want to create a Java application bundle for Mac without using Mac. According
If all tables I want to delete from have the column gamer_id can i

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.