Return query data with PHP and Json

0

Greetings my problem is this, I'm trying to make a query with PHP and jquery, where I send an empty data, just so that it returns a row of the MYSQL database but when I show the data in the HTML it gives me this

[object Object][object Object]

My code Js is as follows

$.getJSON("view_msj.php", function(mensajesd){

        $("#mensajes").append("<div>" + mensajesd + "</div>");

});

And the PHP the next

<?php

try{


    //$nom_msj = $_POST['dt_empys'];

    $base = new PDO("mysql:host=localhost; dbname=quickchat", "root", "");

    $base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $base->exec("SET CHARACTER SET utf8");


//    $resultado=$base->query("SELECT NOMBRE FROM sala WHERE NOMBRE='$nom_msj'");


    $resultado=$base->query("SELECT MENSAJES FROM sala");

    $rows = $resultado->fetchAll(\PDO::FETCH_OBJ);

    echo (json_encode($rows));


}catch(Exception $e){

    echo "Ha habido un error" . $e->GetMessage();
}

? >

    
asked by Brian Hernandez 27.09.2017 в 01:43
source

2 answers

0

$resultado->fetchAll(\PDO::FETCH_OBJ); returns an array. You have to go through each element of the array and show them one by one with a for :

$.getJSON("view_msj.php", function(mensajesd){
   for(var i= 0; i < mensajesd.length;i++)
   {
     $("#mensajes").append("<div>" + mensajesd[i].MENSAJES + "</div>");
   }
});
    
answered by 27.09.2017 / 02:21
source
1

Ready, I could solve it with this

$.getJSON("view_msj.php", function(mensajesd){
   for(var i= 0; i < mensajesd.length;i++)
   {
     $("#mensajes").append("<div>" + mensajesd[i].MENSAJES + "</div>");
   }
});
    
answered by 27.09.2017 в 02:30