How to read $ execute-fetch (PDO :: FETCH_ASSOC)?

0

Good morning stackoverflow community, I just want you to support to know what is my fault in the php code because when doing the while, it does not enter and does not print anything and I saw many pages and nothing has been served, I hope you find the solution

<?php

use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

//Obtener todos los clientes

$app->get('/actividad/{id}', function(Request $request, Response $response){

$id = $request->getAttribute('id');


 $consulta = "SELECT * FROM servicios_inmoviliario_alquiler.actividad where id_actividad = '$id'";

     try{
        // Instanciar la base de datos
        $db = new db();

        // Conexión
        $db = $db->conectar();
        $ejecutar = $db->query($consulta);
        $clientes = $ejecutar->fetch(PDO::FETCH_ASSOC);
        $db = null;

        $header=$_SERVER['HTTP_ACCEPT'];

       if($header == 'application/xml'){

          header('Content-Type: application/xml; charset=utf-8');
          echo ("<actividades>");
          while($row = $ejecutar->fetch(PDO::FETCH_ASSOC)){
             echo("<actividad>");
                echo("<id-actividad>".$row['id_actividad'].</id-actividad>");
                echo("<nombre>".$row['nombre']."</nombre>");
             echo("</actividad>");
          }
          echo("</actividades>");
        }

    } catch(PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});
    
asked by Roberto López 12.12.2017 в 16:30
source

2 answers

0

Make sure you have put all the double quotes. For example in the while, in the second line, you have:

echo("<id-actividad>".$row['id_actividad'].</id-actividad>");

When it should be:

echo("<id-actividad>".$row['id_actividad']."</id-actividad>");

Apart from that you can use a var_dump (); from $ row to see what is returning you.

    
answered by 12.12.2017 в 16:48
0

The method you are looking for is fechArray , not fetch ::

//..

header('Content-Type: application/xml; charset=utf-8');
echo ("<actividades>");
while($row = $ejecutar->fetchArray(PDO::FETCH_ASSOC)){

 //...

According to the documentation of the method fetchArray :

  

Get a row of results in the form of an associative array or   indexed or both. By default, you get both forms.

While the method fetch only returns the first row of the query.

    
answered by 12.12.2017 в 23:41