Jump to content

Recommended Posts

Captura de ecrã 2025-01-17 094327.png

Captura de ecrã 2025-01-17 094406.png

 

Hello

 

I'm having problems regardind the result of a query using PDO, my site have 2 different access, teacher and supervisor. Two supervisors have the same access but one show result and the other one no because of those "[]"

 $stmt = $DB_con->prepare("SELECT v.idVisita, 
                                                e.Escola, 
                                                p.Nome, 
                                                t.Turma,
                                                v.TipoTurma,
                                                v.Destino, 
                                                v.StartDate,
                                                v.EndDate, 
                                                v.TipoTransporte, 
                                                vt.alunosVisita, 
                                                vt.idTurma as idTurma, 
                                                v.TotalAluno,
                                                v.PdfVisita, 
                                                v.Avaliacao, 
                                                v.idProfessor, 
                                                v.Saida, 
                                                v.Chegada, 
                                                v.PlanoOcupacaoAlunos, 
                                                v.PlanoOcupacaoTurmas, 
                                                v.CustoEstadia, 
                                                v.CustoSeguro,
                                                v.CustoTransporte, 
                                                v.TipoSeguro, 
                                                v.TipoEstadia, 
                                                v.TipoTransporte, 
                                                v.idProfessor,
                                                v.Obs, 
                                                v.Areas, 
                                                v.Total,
                                                v.TipoEstadia, 
                                                v.TipoSeguro, 
                                                v.TipoTransporte, 
                                                v.Financiamento,
                                                vt.idTurma,
                                                v.PdfAvaliacao,
                                                v.Estado, 
                                                IF(v.Estado='1','DEF',IF(v.Estado='0','IND','AG')) as EstadoNum, 
                                                v.CustoIngressos, 
                                                v.TipoIngressos,
                                                v.haveClasses,
                                                d.Departamento,
                                                v.percent
                                                    FROM
                                                        visitas AS v
                                                        INNER JOIN escolas AS e ON (v.idEscola = e.idEscola)
                                                        INNER JOIN visitasturmas AS vt ON (v.idVisita = vt.idVisita)
                                                        INNER JOIN turmas AS t ON (vt.idTurma = t.idTurma)
                                                        INNER JOIN professores AS p ON (v.idProfessor = p.idProfessor)
                                                        INNER JOIN anosescolares AS ae ON (vt.idAnoEscolar = ae.idAnoEscolar)
                                                        INNER JOIN grupos g on p.idGrupo = g.idGrupo
                                                        INNER JOIN departamentos d on g.idDepartamento = d.idDepartamento
                                                        WHERE ae.Estado = 1 
                                                        ORDER BY v.idVisita,v.StartDate, t.Turma");
            $stmt->execute();
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            echo json_encode($result);

Why?

How can i remove this?

 

 

Edited by gmc1103
Link to comment
https://forums.phpfreaks.com/topic/326556-strange-result-in-pdofetch_assoc-result/
Share on other sites

Just a thought here. Are the teachers and supervisors saved as an Array in the database? If they are, this could be the issue because PHP has issues displaying data to a certain user based on ID or username or whatever because it also retrieves the data from the database including the [""], for example, ["teacher"], ["supervisor"].

Just a thought.

Hello, thanks for participating

Are the teachers and supervisors saved as an Array in the database?

No

We have 4 supervisors, each one have same access rights and can see the same stuffs.

In this case, 2 supervisors who wants to check all study visits use this query. But as you can the final [] in one gives no data.

Edited by gmc1103
19 hours ago, requinix said:

That is not var_dump output.

What does this output:

$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
var_dump(json_encode($result));

Hello agian

 

Sendin the file with 2 dumps

json_encode start at line 11579

The website don't allow me to put with code tags

But it seems it is the json_encode who is adding [] ate the end.

 

data.txt

Hello again 

 

Iv'e been checking php functions to check the result when i use this

 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

And one of function is sizeof

 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            //echo json_encode(getDadosVisita($result));
            echo sizeof($result);

For one supervisor the echo is 

154[]

And for another supervisor is

154

So now i'm lost...what's is going on to have [] in supervisor and none in another? 

On 1/21/2025 at 12:41 PM, requinix said:

You have something, somewhere in your code that is outputting an empty array. It's not in the code you've shown so far. Try searching your code for usages of json_encode that don't belong.

You are certainly not showing us the actual code you are using, or there is something in your debugging because sizeof/count returns an int.  So the sizeof is not echoing 154[].  Something is echoing 154 and then something else is echoing out the "[]" which is an empty array.

One thought I had is that this issue could have something to do with your query(s) and what is in your database.

A common mistake (and one you are making here) is to assume you will be getting one row in a result set, and then fetching that result with fetchAll.  

If you only expect one row, then the query should reflect that, as well as the code you use to fetch the row(s) from the result.

It occurred to me, that you might be getting what is essentially an empty row in your result set, or getting 2 rows when you only expected one.

I can't say for sure that this is the issue, but it's a possibility, given the snippets of code you've provided so far.  Your query is doing a lot of joins, and any confusion on your part or extra rows in the joined tables will generate additional rows in the result set as a product of the join.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.