원격 mysql 데이터베이스에서 데이터를 가져 와서 json 으로 가져 오기를 시도하고 있습니다. 연결은 정상이며 문제는 최종적으로 json에서 반향하는 것이 정상이 아니라는 것입니다. 이것이 내 PHP 코드입니다.json에 mysql 쿼리 결과를 가져 오기
<?php
require("config.inc.php");
$query = "Select * FROM comments";
$res = mysql_query($query);
$rows = mysql_fetch_assoc($res);
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["username"] = $row["username"];
$post["title"] = $row["title"];
$post["message"] = $row["message"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
이 페이지에 반향 결과입니다
post_id username title message
1 reda test title 2 test message xxx
2 reda2 title 2 message 2
01 :
이
{"success":1,"message":"Post Available!","posts":[{"username":"1","title":"1","message":"1"},{"username":"r","title":"r","message":"r"},{"username":"t","title":"t","message":"t"},{"username":"t","title":"t","message":"t"}]}
내 데이터베이스 테이블 "코멘트"이러한 데이터를 포함하는 동안
도와주세요 감사합니다. 단일 배열, 다차원 배열 루프를 얻기 위해 노력하고있다
foreach ($rows as $row) {
이상한 점은 json이 반환 한 데이터입니다. 데이터베이스의 테이블에있는 데이터와 일치하지 않습니다. – user3870679
{ "성공": 1, "메시지": "게시 가능", "게시물": [{ " "reda", "title": "test title 2", "message": "test message xxx"}, { "username": "reda2", "title": "title 2", "message" "message 2"}]} 이것은 현재 결과 대신에 결과가되어야합니다 – user3870679