php – JSON print me twice the result of a query


I have a query in PHP


public static function mostrarSlideIDModel($datosModel, $tabla){

    $stmt = Conexion::conectar()->prepare("SELECT id_slide, marca_slide, desc_slide, liga_slide, imagen_slide FROM $tabla WHERE id_slide = :id");

    $stmt -> bindParam(":id", $datosModel["id"], PDO::PARAM_INT);

    $stmt -> execute();

    return $stmt -> fetch();

    $stmt -> close();


public static function mostrarSlideIDController(){

        $datosController = array("id"   =>  $_POST['idSlide']);

        $respuesta = SlideModel::mostrarSlideIDModel($datosController, "slides");

        echo json_encode($respuesta);

and when viewing in the browser, in the AJAX response, it prints the information twice, one with an index and the other with the name of the column.



I do not know how correct this can be, they are from the first times that I handle data in JSON. Hopefully someone can guide me, Thank you.


The problem is that the fetch function uses a parameter whose default value returns an associative array and an array with indices. If you want to see only one of these two options, you need to specify the parameter like this:

return $stmt -> fetch(PDO::FETCH_ASSOC);
Scroll to Top