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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:54:27+00:00 2026-06-07T02:54:27+00:00

I am attempting to make an AJAX call to a very small PHP script

  • 0

I am attempting to make an AJAX call to a very small PHP script that should return me an array that could be echo’d and decoded using JQuery. Here is what I have:

My PHP page called to by AJAX:

$web_q=mysql_query("select * from sec_u_g where uid='$id' ");
$rs = array();
while($rs[] = mysql_fetch_assoc($web_q)) {
}
print_r(json_encode($rs));

This outputs:

[{"id":"3","uid":"39","gid":"16"},{"id":"4","uid":"39","gid":"4"},{"id":"5","uid":"39","gid":"5"},{"id":"6","uid":"39","gid":"6"},{"id":"7","uid":"39","gid":"7"},{"id":"8","uid":"39","gid":"8"},{"id":"9","uid":"39","gid":"9"},false] 

I don’t understand the “false” at the end for one .. But then I send to to JQuery and use:

$.each(json.result, function(i, object) {
$.each(object, function(property, value) {
    alert(property + "=" + value);
});
});

This just fails. I try to alert “result” by itself which is set by:

  $.post("get_ug.php",{id:txt},function(result){
  });

My output alerts are as follows:

1)  The key is '0' and the value is '['
2)  The key is '1' and the value is 'f'
3)  The key is '2' and the value is 'a'
4)  The key is '3' and the value is 'l'
5)  The key is '4' and the value is 's'
6)  The key is '5' and the value is 'e'
7)  The key is '6' and the value is ']'
8)  The key is '7' and the value is '
    '    (<-- Yes the line break is there in the alert)

I am exhausted from trying different ideas and scripts. Other than setting a delimiter myself and concatenating my own array and decoding it with a custom script, does anyone have any ideas?? Thank you!!

  • 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-07T02:54:31+00:00Added an answer on June 7, 2026 at 2:54 am

    the false comes from your while loop:

    while($rs[] = mysql_fetch_assoc($web_q))
    

    at the last iteration, mysql_fetch_assoc returns false, which is inserted to $rs[], and thus finds it way to your json.
    This is also what causes your json to be invalid.
    get rid of this false by using a temporary variable for the loop.
    everything should be fine afterwards.

    Edit (Revised code with temporary variable):

    $web_q=mysql_query("select * from sec_u_g where uid='$id' ");
    $rs = array();
    $result; //temporary variable to hold the current 'fetch' result.
    while($result = mysql_fetch_assoc($web_q)) {
        array_push($rs, $result); //push the result into the array only if it
                                  //passed the loop condition.
    }
    print_r(json_encode($rs));
    

    note:
    instead of array_push($rs, $result); you could of course also use $rs[] = $result;.

    Edit 2 (jQuery + json):
    in order to parse the json object, here’s an example of how to build the ajax call:

    $.ajax({
        url: "get_ug.php",
        data: {
            id: txt
        },
        type: "POST",
        dataType: "json", //important (!!) - this makes sure the received data is parsed as json.
        success: function(data){
            //'data' is the resulting json object which is received from your php file.
            //use it to access your data.
            console.log( data );
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to make a php script that can load the current weather forecast
I am attempting to make a pie chart using a php file that gets
I'm attempting to make a cross domain script call to get some JSON data
I'm attempting to use AJAX to call make a SOAP call (not cross-site, so
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm attempting to make a simple linear text game that will display inside a
Im attempting to make a little app that lets you add text boxes to
I'm attempting to make a custom ContentProvider so that more than one application (activity?)
I am attempting to make a simple class that serializes itself to disk when
I am attempting to make a jsonp call with jQuery 1.7 but when the

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.