Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dru's avatar
Level 3

getting different results in phpMyAdmin AND laravel with same query

I copy and paste the query from laravel to phpmyadmin and I get a different result, obviously it is the same database. why is this happening?

I have this code:

dump($usuario_id,$prueba_id );
        $resultados = DB::select(DB::raw('SELECT
                                smrt_preguntas.grupo_pregunta AS eval_grupo_id,
                                SUM(smrt_respuestas.puntaje) AS valor,
                                GROUP_CONCAT(DISTINCT smrt_usuarios_pruebas.usuario_id) as usuarios_id,
                                COUNT(DISTINCT smrt_usuarios_pruebas.usuario_id) as total_usuarios

                                FROM
                                    smrt_usuarios_pruebas_respuestas
                                INNER JOIN smrt_preguntas ON smrt_preguntas.id = smrt_usuarios_pruebas_respuestas.pregunta_id
                                INNER JOIN smrt_usuarios_pruebas ON smrt_usuarios_pruebas.id = smrt_usuarios_pruebas_respuestas.usuario_prueba_id
                                INNER JOIN smrt_respuestas ON smrt_usuarios_pruebas_respuestas.respuesta_id = smrt_respuestas.id
                                WHERE
                                    smrt_usuarios_pruebas.usuario_id IN ( :usuario_id )
                                    AND smrt_usuarios_pruebas.id IN ( :prueba_id )
                                    AND smrt_usuarios_pruebas.estado_id = 4
                                    AND smrt_usuarios_pruebas.activo = 1
                                    AND smrt_usuarios_pruebas.eliminado = 0
                                GROUP BY
                                smrt_preguntas.grupo_pregunta ;'),[ 'prueba_id'=>$prueba_id, 'usuario_id'=>$usuario_id ]);
    }
dd('asd', $resultados, $resultados[0]->total_usuarios);

this is the output:

"30736,41659"
"21,37"
"asd"
array:12 [▼
  0 => {#416 ▼
    +"eval_grupo_id": 1
    +"valor": 45.0
    +"usuarios_id": "30736"
    +"total_usuarios": 1
  }
  1 => {#417 ▼
    +"eval_grupo_id": 2
    +"valor": 30.0
    +"usuarios_id": "30736"
    +"total_usuarios": 1
  }
  2 => {#418 ▼
    +"eval_grupo_id": 3
    +"valor": 43.0
    +"usuarios_id": "30736"
    +"total_usuarios": 1
  }
  3 => {#419 ▶}
  4 => {#420 ▶}
  5 => {#421 ▶}
  6 => {#422 ▶}
  7 => {#423 ▶}
  8 => {#424 ▶}
  9 => {#425 ▶}
  10 => {#426 ▶}
  11 => {#427 ▶}
    ]
0 likes
8 replies
bobbybouwmann's avatar

I can't help you without more context. What is the difference between the results? What do you expect exactly?

1 like
dru's avatar
Level 3

the thing is, that the result that is in phpMyAdmin is totally different.

in:

array:12 [▼
  0 => {#416 ▼
    +"eval_grupo_id": 1 //all this is wrong
    +"valor": 45.0 //all this is wrong
    +"usuarios_id": "30736" // in here it should have borught this: 30736,41659
    +"total_usuarios": 1 //it should have been: 2
  }

So I don't understand why sometimes those queries bring a diferent result if I try the query in a database tool

jlrdw's avatar

Do a

echo '<pre>';
print_r($your qyery here);
echo '</pre>';
exit();

May help you see results better.

bobbybouwmann's avatar

Well, that is strange indeed.

But I have no clue about the data. The only thing I can think of is that you're querying the wrong database, or you use different values in the where statements.

1 like
Dalma's avatar

Are you connecting to the same database?

dru's avatar
Level 3

@dalma yes it is, I thought that could have been the problem.

is there a way to make the variables in the sql string to appear? like when you to ->toSQL(); it shows you the query, but is there a way to make the :variables show the value? because I think that for some reason the value of the variable is not changing

ahmeddabak's avatar

you can use the dd function to debug the query, the variables should be there too.

  DB::table('clients')->dd();

  Client::query()->dd();

Please or to participate in this conversation.