简体   繁体   中英

Retrieving data from a database with ajax

Why can't I get all data from database when it is looped already? I'm only getting the last row of data from database.

HTML:

<div id="registerlist">
</div>

        <script>
        $.getJSON("http://url/getapplicantdetails.php?callback=?",{},function(data)
            {
            var rlist = (data.fullname);
            var fullndear = document.getElementById('registerlist');
            fullndear.innerHTML = rlist;

            });
        </script>

PHP:

<?php 
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json");

include_once ("dbcon.php");

    $sql = "SELECT * FROM tblregistered";
    $select = mysql_query($sql);
    while($row = mysql_fetch_array($select))
 {
 echo $_GET['callback']."  (".json_encode(array("fullname"=>$row['fullname'],"email"=>$row['email'],"username"=>$row['username'],)).");";
 }
 ?>

Anyone please tell me what am I missing or what's wrong. Thanks in advance.

You are displaying the data in the browser more records that's wrong display json multidimensional array and process it in the loop.

<?php 
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json");

include_once ("dbcon.php");

//local array
$data = array();
    $sql = "SELECT * FROM tblregistered";
    $select = mysql_query($sql);
    while($row = mysql_fetch_array($select))
 {

    //fill the local array
    $data[] = array("fullname"=>$row['fullname'],"email"=>$row['email'],"username"=>$row['username']);


 }

 //display the output
 echo json_encode($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM