-1

php file:

   <?php
mysql_connect("127.0.0.1");
mysql_select_db("test"); 
$sql=mysql_query("select first,last,email,city from userdata"); 
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output)); 
   mysql_close(); 
?>

json output:

[{"first":"nag","last":"ch","email":"[email protected]","city":"guntur"},{"first":"hari","last":"ch","email":"[email protected]","city":"guntur"}] 

html code:

 <script>
    $(document).show(function(){
    });

        $("#userdata tbody").html("");
        $.getJSON("http://127.0.0.1/reg/userdata.php",function(data){
              $.each(data,function(i,user){
                    var tblRow =
                        "<tr>"
                          +"<td>"+user.first+"</td>"
                          +"<td>"+user.last+"</td>"
                          +"<td>"+user.email+"</td>"
                          +"<td>"+user.city+"</td>"
                        +"</tr>"
                    $(tblRow).appendTo("#userdata tbody");
                });
            }
        );
    </script>

i was displayed data from database using php in JSON format..i want to insert this data into table format in html page, i created table in html also..i tried using above script but it didn't fetch any data..plz help me plz..

1
  • Have you actually confirmed that JSON output? I am not sure how you are getting all those results from that select query - I would expect to see a single field for each array entry. Commented Jun 28, 2012 at 6:55

2 Answers 2

0

Your PHP is printing the array in an incremented format.

Make the following changes:

print(json_encode($row)); 

Next, there is no userdata in the output, so just use data.

6
  • am not getting output...it shows empty table, will u plz modify the code plz Commented Jun 28, 2012 at 9:54
  • @user1310385, I would to be happy to fix the problem. Create your issue in jsfiddle.net and post the link :) Commented Jun 28, 2012 at 10:12
  • now i am using localhost..can we create demo even if u are using localhost. Commented Jun 28, 2012 at 10:40
  • Just use the js and html portion, the php is not required to show the issue i guess. Commented Jun 28, 2012 at 10:56
  • i have sent the link plz check it once Commented Jun 28, 2012 at 11:34
0

According by your php code, the data is an array, and there is no userdata property.

$.each(data.userdata, function(i,user){

should be

$.each(data, function(i,user){

THE DEMO.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.