Error accessing array indices by name( foreach-php )

Asked

Viewed 89 times

-2

I am not being able to access the indexes of an array, which is the result of a query, through the column name, only by the corresponding numbers.

<?php

$conexao = mysqli_connect("localhost", "root", "", "bdteste");

function verificaVencidos($conexao){

$query = "select * from tbl_atividades where  atualizado <> 'S'";

$resultado = mysqli_query($conexao, $query);
$lista_resultado = array();

while($pre_lista = mysqli_fetch_array($resultado)){
    array_push($lista_resultado, $pre_lista);  
}
    return $lista_resultado;
}


$resultados = verificaVencidos($conexao);

foreach($resultados as $print){

    echo $print[1] . "<BR>";
    echo $print[2] . "<BR>";
    echo $print[3] . "<BR>";
    echo $print[4] . "<BR>";
    echo $print[5] . "<BR>";
    echo $print[6] . "<BR>";
    echo $print[7] . "<BR>";
    echo $print[8] . "<BR>";
    echo $print[9] . "<BR>";
    echo $print[10] . "<BR>";
    echo $print[11] . "<BR>";
    echo $print[12] . "<BR>";
}

wish to access in this way:

<?php

$conexao = mysqli_connect("localhost", "root", "", "bdteste");

function verificaVencidos($conexao){

$query = "select * from tbl_atividades where  atualizado <> 'S'";

$resultado = mysqli_query($conexao, $query);
$lista_resultado = array();

while($pre_lista = mysqli_fetch_array($resultado)){
    array_push($lista_resultado, $pre_lista);  
}
    return $lista_resultado;
}


$resultados = verificaVencidos($conexao);

foreach($resultados as $print){

    echo $print['tipo_atividade'] . "<BR>";

}

but when I put it like this returns the following error:

Notice: Undefined index: tipo_atividade in 
C:\xampp\htdocs\atualizador\atualizador-beta.php on line 23

Already I stopped other topics with similar subject, but none gave solution.

print_r result:

Array ( [0] => 93233 [c digo] => 93233 [1] => 696 [COD] => 696 [2] => 3 FG ADMINISTRA O (CORDEIRO) [EMPRESAS] => 3 FG ADMINISTRA O (CORDEIRO) [3] => PRESUMIDO [TRIBUTACAO] => PRESUMIDO [4] => PEDRO [RESPONSAVEL] => PEDRO [5] => GLEISI. R [DESIGNATOR] => GLEISI. R [6] => DCTF [TIPO_ATIVIDADE] => DCTF [7] => [DT_INICIO] => [8] => 2019-09-24 00:00:00 [DT_VENCIMENTO] => 2019-09-24 00:00:00 [9] => [DT_FIM] => [10] => PENDING [STATUS] => PENDING [11] => [DETAILS] => [12] => [FEEDBACK] => [13] => ACCOUNTING [DEPARTMENT] => ACCOUNTING [14] => MATRIX [EMPRESA_ORIGEM] => MATRIX [15] => [DATA_VISUALIZADO] => [16] => [DATA_FEEDBACK] => [17] => S [ARCHIVE] => S [18] => [DIRECTORY] => [19] => C [EXTRA_URGENTE_COMUM] => C [20] => N [VIEWED] => N [UPDATED] => N [22] => [DATA_DETALHES] => [23] => TO CHECK OUT [CONFERENCIA] => A CONFERIR [24] => [CONFERENTE] => [25] => 1 [QNTD_PROCESSOS] => 1 [26] => [EXTRA_VISUALIZADO] => [27] => [FEDBACK_VISUALIZADO] => [28] => [URGENTE_VISUALIZADO] => [29] => [OBSERVACOES_PRIVADAS] => [30] => N [DETALHE_VISALIALIZADO] => N [31] => N [OBSERVACOES_PRIVADAS_VISUALIZADAS] => )

  • And there’s the column tipo_atividade in the database? What is the result of print_r($print)?

  • Hello! I believe you have to use aspas duplas, I checked in the official PHP documentation for the function mysqli_fetch_array and there presents the use of aspas dupla. Below is the documentation link PHP Doc

  • Yes @Andersoncarloswoss just checked this my error, I haven’t looked at the code undo! already delete my comment.

  • 1

    @Andersoncarloswoss does exist, I can access it so if in select I instead of putting "select * from" put select type_activity from". The result of the print_r is all query data: Array ( [0] => 93233 [c digo] => 93233 [1] => 696 [COD] => 696 [2] => 3 FG ADMINISTRA O (CORDEIRO) [EMPRESAS] => 3....

  • 1

    @Even Nicolaspereira with double quotes won’t go. It will only, as I said, if I put "select tipo_activity from" instead of "select * from", but I need to get all the fields in the query.

  • @Denied Can put this output from the print_r in the question?

  • @Denied you running the SELECT * FROM and when printing the variable you put the index of it, right? example, run the SELECT * FROM and in the echo leave as follows: echo $print[indexDoCampo]."<br>";

  • 1

    @Nicolaspereira you say that way? echo $print[1] ? if yes, so it goes.

  • 1

    Are you sure the column name is all tiny anyway? Wouldn’t it be TIPO_ATIVIDADE?

  • 1

    I believe the @Andersoncarloswoss solution will work, I believe the field in the database is TIPO_ATIVIDADE

  • 1

    @Andersoncarloswoss guy, that’s right. I’m familiar with VBA that the query is not case sensitive, and also accessed by the index of the array. Thank you very much for your answers!

Show 6 more comments

1 answer

1


Remember that PHP is case sensitive; if you create in the database the column TIPO_ATIVIDADE then PHP will generate the key in array also with uppercase text. It makes a difference to access the positions TIPO_ATIVIDADE, tipo_atividade, Tipo_Atividade, etc. Be very careful with this.

Another thing, your tie to generate one array from the results is completely unnecessary:

$resultado = mysqli_query($conexao, $query);
$lista_resultado = array();

while($pre_lista = mysqli_fetch_array($resultado)){
    array_push($lista_resultado, $pre_lista);  
}

return $lista_resultado;

Instead use mysqli_fetch_all that will already return all the records in one array:

$resultado = mysqli_query($conexao, $query);

return mysqli_fetch_all($resultado);

And yet, if you only need the associative form of the result, you can inform the function not to generate the numeric indices:

return mysqli_fetch_all($resultado, MYSQLI_ASSOC);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.