# Cakephp return a rare json data with my custom query

**URL:** <https://discourse.cakephp.org/t/cakephp-return-a-rare-json-data-with-my-custom-query/937>\
**Category:** Need Help\
**Tags:** behavior\
**Created:** [August 9, 2016, 6:29am UTC](https://discourse.cakephp.org/t/cakephp-return-a-rare-json-data-with-my-custom-query/937 "2016-08-09T06:29:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgagneten](https://avatars.discourse-cdn.com/v4/letter/f/a5b964/32.png) [@fgagneten](https://discourse.cakephp.org/u/fgagneten)\
**Post date:** [August 9, 2016, 6:29am UTC](https://discourse.cakephp.org/t/cakephp-return-a-rare-json-data-with-my-custom-query/937/1 "2016-08-09T06:29:07Z")

</div>

I have this function in my controller:

```
public function estadisticas()
{
    $conn = ConnectionManager::get('default');
    $stmt = $conn->execute(
        'SELECT c.nombre, 
       c.idcomplejo, 
       t.canchafk, 
       Count(*) AS cantidadturnos 
FROM turno t 
INNER JOIN usuario u ON u.idusuario = t.usuariofk
INNER JOIN cancha ca ON t.canchafk = ca.idcancha 
INNER JOIN complejo c ON c.idcomplejo = ca.complejofk 
INNER JOIN ciudad cc ON cc.idciudad=c.ciudadfk
WHERE u.escomplejo=0 
AND cc.paisfk="1" 
AND u.paisfk="1" 
AND noasistio=0 
AND fecha BETWEEN "2016-07-01" AND "2016-07-30" 
GROUP BY canchafk');
    

    $turnos = $stmt ->fetchAll('assoc');
    
    $this->set('turnos',$turnos);
    

}

```

I need to make custom query because is dificult to make it like cakephp said.  
So the result is for each “turnos”:

```
array(4) { ["nombre"]=> string(7) "Campnou" ["idcomplejo"]=> string(1) "1" ["canchafk"]=> string(1) "1" ["cantidadturnos"]=> string(2) "35" } 

array(4) { ["nombre"]=> string(6) "Uñazo" ["idcomplejo"]=> string(1) "3" ["canchafk"]=> string(1) "6" ["cantidadturnos"]=> string(2) "29" }

```

Like this the area 17 more arrays.

When I do this:

```
 <?php foreach ($turnos as $turno): ?>
        <tr>
            <br><?php var_dump($turno) ?></br>
            <td><?= h($turno['idComplejo']) ?></td>
            <td><?= h($turno['canchaFK']) ?></td>
            <td><?= h($turno['cantidadTurnos']) ?></td>
        </tr>
        <?php endforeach; ?>

```

The rows are empty, but cakephp show me 19 empty row (the total rows of the query) .In phpmyadmin the query works perfect.

So… I have a problem try to showing the data from the json.

Thanks for help

---

<div class="post-metadata">

**Author:** ![cvl\_75](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/cvl_75/32/355_2.png) [@cvl\_75](https://discourse.cakephp.org/u/cvl_75)\
**Post date:** [August 9, 2016, 2:36pm UTC](https://discourse.cakephp.org/t/cakephp-return-a-rare-json-data-with-my-custom-query/937/2 "2016-08-09T14:36:35Z")

</div>

In dump I see [“idcomplejo”], but in foreach you have h($turno[‘idComplejo’]).  
What is var\_dump($turno) dumps? (BTW, put it into a `<td></td>` )
