我试图从JSON中的数据库中获取我的表,帮助我

时间:2015-07-10 16:45:26

标签: php json

我的PHP代码。

<?php
//open connection to mysql db
$connection = mysqli_connect("host_name","user_name","my_password","database_name") or die("Error " . mysqli_error($connection));

//fetch table rows from mysql db
$sql = "select * from mytable";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//create an array
$emparray[] = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
echo json_encode($emparray);

//close the db connection
mysqli_close($connection);
?>

它输出为:`

[[],{"id":"1","name":"vikash","rollno":"39"},{"id":"2","name":"tausif","rollno":"35"},{"id":"3","name":"sharma","rollno":"30"}]

输出中有一个额外的数组,不应该在那里。 我无法找到原因。

1 个答案:

答案 0 :(得分:3)

替换以下行:

$emparray[] = array();

用这个:

$emparray = array();