使用ajax从数据库中检索数据

时间:2014-02-01 13:57:12

标签: php ajax

为什么我不能从数据库中获取所有数据?我只从数据库中获取最后一行数据。

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'],)).");";
 }
 ?>

任何人都请告诉我我错过了什么或错了什么。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您正在浏览器中显示更多错误的数据显示json多维数组并在循环中处理它。

<?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);