1

Estoy reemplazando un código antiguo de un articulo que encontré, el siguiente código es el encargado de crear datos json

<?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);
?>

Y este imprime el siguiente resultado:

[{"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}]

Ahora en el proceso de reemplazo tengo el siguiente código:

<?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);

Y este me imprime el siguiente resultado:

{"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}

Es casi perfecto, hasta pensé que si funcionaría pero lamentablemente no funciono, a mi código json le hace falta los corchete [] de apertura y cierre, ademas de separar por coma cada cierre de las llaves }, y cada uno de los datos separados por comillas "".

Me pueden explicar que me hace falta para lograr obtener los datos json como el antiguo código.

1 respuesta 1

3

Basta con generar tu mismo los corchetes de inicio y final, y añadir la coma de separación; todo a mano:

echo '[';
$count = 0;

while( $stmt->fetch( ) ) {
  if( $count ) {
    echo ',';
  }

  echo json_encode( $member );

  ++$count;    
}

echo ']';
1
  • 1
    Fantástico amigo, eres un genio :) muchas gracias.
    – Lou
    Commented el 14 abr. 2019 a las 7:01

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.