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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:45:36+00:00 2026-06-03T22:45:36+00:00

I have a mysql database that stores data such as usernames. I have a

  • 0

I have a mysql database that stores data such as usernames. I have a PHP file called result which retrieves the results from the database and encodes them into a JSON string. I then want the same PHP file to output the results without a page reload (via ajax). Effectively I want the same page to get the JSON string as a HTTP request, how do I go about this

this is results.php so far

            <?php

        $link = mysql_connect('', '', '');
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }

        $db_selected = mysql_select_db('test', $link);
        if (!$db_selected) {
            die ('Can\'t use test : ' . mysql_error());
        }

        $result = mysql_query("SELECT user_name FROM users");
        if (!$result) {
            die('Invalid query: ' . mysql_error());
        }


        $names = array();

        while ($row = mysql_fetch_assoc($result)) {

            foreach ($row as $key => $val) {

                $names[][$key] = $val;
            }
        }

        if ($_GET['myvar'] == "done")
        {
            $ennames = json_encode($names);
            echo $ennames;
            exit();
        }




        ?>
         <script src="jquery-1.7.1.min.js">

         function getJSON()
         {
            $.getJSON("results.php", { myvar: "done" }, function(data) {
               var namesHTML = "";

               $.each(data, function(key, val) {
                   namesHTML = namesHTML + val + "<br/>";
                 });

               $("#divForNames").html(namesHTML);
            });
            setTimeOut(getJSON, 5000); //the 5000 here means 5 seconds
         }
        </script>

         <div id="divForNames">

         </div>
  • 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-06-03T22:45:38+00:00Added an answer on June 3, 2026 at 10:45 pm

    The only adjustment that your PHP will need for this is to add an if statement immediately after the assignment of $ennames:

    if ($_GET['myvar'] == "specific-value-that-you-choose")
    {
       echo $ennames;
       exit();
    }
    

    For the polling, you will need to do some kind of client-side script – most likely javascript, and most easily written in jQuery. Look into the .getJSON() jQuery function and the setTimeOut() to create logic to do this polling. It will probably look something like this:

    function getJSON()
    {
       $.getJSON("result.php", { myvar: "specific-value-that-you-choose" }, function(data) {
          var namesHTML = "";
    
          $.each(data, function(key, val) {
              namesHTML = namesHTML + val + "<br/>";
            });
    
          $("#divForNames").html(namesHTML);
       });
       setTimeOut(getJSON, 5000); //the 5000 here means 5 seconds
    }
    

    Then somewhere in the HTML for the non-JSON page, you need to include a <div> with the id set to match the one you using inside the $.getJSON() function (in this case, I’d need an empty <div id="divForNames"> but you can change the names to fit your preferences.

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

Sidebar

Related Questions

I have a php page that pulls data from a mysql database based on
I have a MySql database that stores a timestamp for each record I insert.
I have a script that automatically populates a mysql database with data every hour.
I am creating a PyQt application that stores its data in a MySQL database.
I have developed a web application driven by a mysql database that stores information
I have a database that stores data as UTF-8, but sometimes some data (outside
I have a simple two-field form that stores its data in the database. For
I have a database that stores image urls andother data, I want a gallery
Ok I have a table in my SQL Server database that stores comments. My
I have a mysql database that is tracking hockey stats. What I'd like to

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.