I am replacing an old code from an article I found, the following code is responsible for creating datajson
<?php
require_once ("Conexion.php");
$memberId = 1;
$sql = "SELECT comentario.*,megusta_nomegusta.like_unlike FROM comentario LEFT JOIN megusta_nomegusta ON comentario.comentario_id = megusta_nomegusta.comentario_id AND member_id = " . $memberId . " ORDER BY parent_comentario_id asc, comentario_id asc";
$result = mysqli_query($conn, $sql);
$record_set = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($record_set, $row);
}
mysqli_free_result($result);
mysqli_close($conn);
echo json_encode($record_set);
?>
And this prints the following output:
[{"comentario_id":"14","parent_comentario_id":"0","comment":"The system coment is perfect!","comment_sender_name":"Lou","date":"2019-04-14 05:45:09","like_unlike":null},
{"comentario_id":"18","parent_comentario_id":"0","comment":"","comment_sender_name":"","date":"2019-04-14 06:23:15","like_unlike":null},
{"comentario_id":"19","parent_comentario_id":"0","comment":"My new code php","comment_sender_name":"YouLive","date":"2019-04-14 06:28:33","like_unlike":null},
{"comentario_id":"20","parent_comentario_id":"0","comment":"j","comment_sender_name":"j","date":"2019-04-14 06:36:09","like_unlike":null},
{"comentario_id":"21","parent_comentario_id":"0","comment":"aaa","comment_sender_name":"aaaaaaa","date":"2019-04-14 06:38:34","like_unlike":null},
{"comentario_id":"15","parent_comentario_id":"14","comment":"Yes","comment_sender_name":"Mario","date":"2019-04-14 05:46:23","like_unlike":null},
{"comentario_id":"16","parent_comentario_id":"15","comment":":)","comment_sender_name":"Lou","date":"2019-04-14 05:46:45","like_unlike":null},
{"comentario_id":"17","parent_comentario_id":"16","comment":"Only allow. three levels of comment. ** This comment should not exist in fourth level **","comment_sender_name":"Lou","date":"2019-04-14 05:57:41","like_unlike":null}]
Now in the process of replacing I have the following code:
<?php
$memberId = 1;
$stmt = $conn->prepare("SELECT comentario.*,megusta_nomegusta.like_unlike FROM comentario LEFT JOIN megusta_nomegusta ON comentario.comentario_id = megusta_nomegusta.comentario_id AND member_id=? ORDER BY parent_comentario_id ASC, comentario_id ASC");
$stmt->bind_param("i",$memberId);
$stmt->execute();
$stmt->store_result();
$member = array();
$stmt->bind_result(
$member['comentario_id'],
$member['parent_comentario_id'],
$member['comment'],
$member['comment_sender_name'],
$member['date'],
$member['id']
);
while ($stmt->fetch()) {
//array_push($member);
echo json_encode($member);
}
//echo json_encode($member);
And this prints the following output:
{"comentario_id":14,"parent_comentario_id":0,"comment":"The system coment is perfect!","comment_sender_name":"Lou","date":"2019-04-14 05:45:09","id":null}
{"comentario_id":18,"parent_comentario_id":0,"comment":"","comment_sender_name":"","date":"2019-04-14 06:23:15","id":null}
{"comentario_id":19,"parent_comentario_id":0,"comment":"My new code php","comment_sender_name":"YouLive","date":"2019-04-14 06:28:33","id":null}
{"comentario_id":20,"parent_comentario_id":0,"comment":"j","comment_sender_name":"j","date":"2019-04-14 06:36:09","id":null}
{"comentario_id":21,"parent_comentario_id":0,"comment":"aaa","comment_sender_name":"aaaaaaa","date":"2019-04-14 06:38:34","id":null}
{"comentario_id":15,"parent_comentario_id":14,"comment":"Yes","comment_sender_name":"Mario","date":"2019-04-14 05:46:23","id":null}
{"comentario_id":16,"parent_comentario_id":15,"comment":":)","comment_sender_name":"Lou","date":"2019-04-14 05:46:45","id":null}
{"comentario_id":17,"parent_comentario_id":16,"comment":"Only allow. three levels of comment. ** This comment should not exist in fourth level **","comment_sender_name":"Lou","date":"2019-04-14 05:57:41","id":null}
It is almost perfect, I even thought that it would work but unfortunately it did not work, my code json
lacks the []
opening and closing brackets, in addition to separating each closing of the braces },
by commas and each of the data separated by quotes ""
.
Can you explain to me what I need to get the data json
like the old code.