Paste mysql field dynamically

4

I created a select command based on information sent by the user, eg:

$anoini = $_GET['anoini']; //variavel com ano escolhido pelo usuário
$anofim = 2016; //ano final é 2016

$s = 'SELECT pa_proc_id, procedimento, '; //inicio da query
while ($anoini < $anofim) { //laço para listar todos os anos
    $s = $s . 'SUM( pa_qtdapr * ( 1 - ABS( SIGN( CONVERT( SUBSTRING( pa_cmp, 1, 4 ) , signed ) - '.$anoini.' ) ) ) ) AS a'.$anoini.', '; //a cada ano, ele inclui uma linha na query
    $anoini++;
}
$s = $s . 'SUM( pa_qtdapr * ( 1 - ABS( SIGN( CONVERT( SUBSTRING( pa_cmp, 1, 4 ) , signed ) - '.$anofim.' ) ) ) ) AS a'.anofim.' '; //finaliza criando a linha com o ano atual
$s = $s . 'FROM sia WHERE pa_ufmun = "'.$ibge.'" and substring(pa_cmp,1,4) >= :anoini GROUP BY pa_proc_id'; //finaliza a query

The above code works perfectly and generates a result with some dynamically created fields referring to each year from the one indicated by the user until 2016, eg: a2014, a2015, a2016

I run the query and create the array:

$lista = $db->prepare($s) or trigger_error($db->error);
$lista->bindValue(":anoini",$GLOBALS['anoini']);
$lista->execute();
$GLOBALS['procedimento'] = $lista->fetchAll(PDO::FETCH_ASSOC);

My question is: how do I know what fields (a2014, a2015, etc) were created and how to echo them?

EDITED

I gave a var_dump and the result was this below:

array (size=130)
  0 => 
    array (size=9)
      'pa_proc_id' => string '0101010010' (length=10)
      'procedimento' => string 'ATIVIDADE EDUCATIVA / ORIENTACAO EM GRUPO NA ATENCAO BASICA ' (length=60)
      'a2010' => string '0' (length=1)
      'a2011' => string '0' (length=1)
      'a2012' => string '0' (length=1)
      'a2013' => string '0' (length=1)
      'a2014' => string '0' (length=1)
      'a2015' => string '0' (length=1)
      'a2016' => string '67' (length=2)
  1 => 
    array (size=9)
      'pa_proc_id' => string '0101020015' (length=10)
      'procedimento' => string 'ACAO COLETIVA DE APLICACAO TOPICA DE FLUOR GEL' (length=46)
      'a2010' => string '0' (length=1)
      'a2011' => string '0' (length=1)
      'a2012' => string '0' (length=1)
      'a2013' => string '0' (length=1)
      'a2014' => string '0' (length=1)
      'a2015' => string '0' (length=1)
      'a2016' => string '23' (length=2)
  2 => 
    array (size=9)
      'pa_proc_id' => string '0101020031' (length=10)
      'procedimento' => string 'ACAO COLETIVA DE ESCOVACAO DENTAL SUPERVISIONADA' (length=48)
      'a2010' => string '0' (length=1)
      'a2011' => string '0' (length=1)
      'a2012' => string '0' (length=1)
      'a2013' => string '0' (length=1)
      'a2014' => string '0' (length=1)
      'a2015' => string '0' (length=1)
      'a2016' => string '184' (length=3)

How to list the field names, not values (in this case from a2010 through to2016)?

Hugs.

    
asked by anonymous 04.11.2016 / 19:39

2 answers

3

RESOLVI:

I use the following code in loop to know if the fields exist and I echo them.

if (isset($linha[0]['a'.$anoini])){
    echo $linha[0]['a'.$anoini];
}

A hug to everyone who helped

    
04.11.2016 / 20:08
0

You will loop and within this loop will bring the result into your echo.

Example:

while($rs = $lista->fetchAll(PDO::FETCH_ASSOC)){
  $var1 = $rs['campo_da_tabela'];
  ...
}
    
04.11.2016 / 19:46