我的PHP代碼看起來像這樣如何在PHP mysqli中編碼JSON輸出?
test.php的
<?php
$connect = mysqli_connect("","","","");
global $connect;
if (isset($_POST['login']))
{
$login = $_POST['login'];
$sql = "SELECT * FROM table WHERE login='$login'";
$result = mysqli_query($connect, $sql);
if ($result && mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_array($result)) {
$login_db = $row['login'];
$real_namedb = $row['real_name'];
$email_db = $row['email'];
$dept_db = $row['dept'];
$division_db = $row['division'];
$output= array('messages' => '1',
'login' => $login_db,
'real_name' => $real_namedb,
'email' => $email_db,
'dept' => $dept_db,
'division' => $division_db
);
echo json_encode($output);
exit();
}
mysqli_free_result($result);
}
else {
$output = array('messages' => '2', 'login' => 'wrong credentials from PHP code!');
echo json_encode($output);
echo mysqli_error($connect);
exit();
}
}
else
{
$output = array('messages' => '3', 'login' => 'No post data');
echo json_encode($output);
exit();
}
?>
<!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<form action="test.php" method="post">
<table>
<tr>
<td><i class="fa fa-unlock-alt"></i> </td>
<td>Email : </td>
<td><input type ="text" name="login" size="30"></td>
</tr>
</table>
<p><input type ="submit" name="Submit" value="DISPLAY"> </p>
</form>
</body>
</html>
我上面這樣
{"messages":"1","login":"ID0111","real_name":"NAME HERE","email":"[email protected]","dept":"IT","division":"MDO"}
{"messages":"1","login":"ID0112","real_name":"NAME HERE2","email2":"[email protected]","dept":"IT","division":"MDO"}
我的問題顯示JSON輸出代碼是如何修改上面的PHP代碼,以使JSON輸出顯示是這樣的嗎?我的意思是單點登錄有多個「消息」值
{
"login":"ID0111",
"real_name":"NAME HERE",
"messages":
[
{
"refno":"1234",
"email":"[email protected]",
"dept":"IT",
"division":"MDO"
},
{
"refno":"1345",
"email":"[email protected]",
"dept":"IT",
"division":"MDO"
},
]
}
感激,如果有人可以提供幫助。謝謝。
你'exit'內環路。我不明白你如何獲得2行輸出。 –
因爲他已經編碼的JSON兩次 –