【发布时间】:2016-03-08 00:29:14
【问题描述】:
一直在看这个,它似乎很简单,但由于某种原因我无法掌握它。
我现在的代码输出如下:
{"society_id":1,"name":"TestName1","email":"Test@email1","description":"TestDes1"}
{"society_id":2,"name":"TestName2","email":"Test@email2","description":"TestDes2"}
但我需要是这样的:
[{"society_id":1,"name":"TestName1","email":"Test@email1","description":"TestDes1"},
{"society_id":2,"name":"TestName2","email":"Test@email2","description":"TestDes2"}]
有人能指出我正确的方向吗?我对 PHP 很陌生。
<?php
$user = 'root';
$pass = '';
$db = 'uopuser';
$con=mysqli_connect('localhost', $user, $pass, $db) or die('Unable to connect');
$statement = mysqli_prepare($con, 'SELECT * FROM society');
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $society_id, $name, $email, $description);
$society = array();
while(mysqli_stmt_fetch($statement)){
$society['society_id'] = $society_id;
$society['name'] = $name;
$society['email'] = $email;
$society['description'] = $description;
echo json_encode($society);
}
echo json_encode($society);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
【问题讨论】: