Problem in mysql count php, it works in phpmyadmin and not in php

2

I have this function that should bring me the account of the records in my database on my page:

function cuentaticketspendienteempleado($conexion,$id){
    $pendientes = mysqli_query($conexion, "SELECT COUNT(t.id) as contador_tickets, t.id as id_ticket, u.id as user_id, t.fecha_creacion as t_fcreacion, t.hora_creacion as t_hcreacion FROM ticket as t JOIN usuario AS u ON t.id_usuario = u.id ") or die("Error listando Ticket: ".mysqli_error($conexion));
    return $consulta;
}

I do the query in the phpmyadmin with the following line of code:

  

SELECT COUNT (t.id) as counter_tickets, t.id as id_ticket, u.id as user_id, t.date_creation as t_fcreation, t.hour_creation as t_hcreation FROM ticket as t JOIN user AS or ON t.id_user = u .id

Now, when I try to show it on my page by calling it:

<?php
    $resultadoss = $ticket->cuentaticketspendienteempleado($conexion,$id);
    if($resultadoss > 0) // validamos si es mayor a 0
    {
        echo '<div id="notificacion">',$resultadoss,'</div> '; 
    } else {
        echo 'GG';
    }
?>

The GG appears that it should not.

What can be the error?

After the Ernesto code:

function cuentaticketspendienteempleado($conexion,$id){
    $pendientes = mysqli_query($conexion, 
                "SELECT COUNT(t.id) as contador_tickets, 
                 t.id as id_ticket, u.id as user_id, t.fecha_creacion as
                 t_fcreacion, t.hora_creacion as t_hcreacion
                 FROM ticket  as t JOIN usuario AS u ON t.id_usuario = u.id ") 
                or die("Error listando Ticket: ".mysqli_error($conexion));

     $consulta = mysqli_fetch_assoc($pendientes);

    return $consulta['contador_tickets'];
    }

I'll have to be careful with spelling errors ...

Going back to the subject.

When I use the following code wanting to bring all the tickets that are of my same id, it does not return any value to me.

function cuentaticketspendienteempleado($conexion,$id){
    $pendientes = mysqli_query($conexion, 
                "SELECT COUNT(*) as contador_tickets FROM ticket  as t WHERE t.id_usuario = '$id' ") 
                or die("Error listando Ticket: ".mysqli_error($conexion));

     $consulta = mysqli_fetch_assoc($pendientes);

    return $consulta['contador_tickets'];
    }

    
asked by Juan Ortiz 25.04.2018 в 17:35
source

1 answer

4

It seems that you are returning a variable that you do not use within the function. Try this way:

function cuentaticketspendienteempleado($conexion,$id){
    $pendientes = mysqli_query($conexion, 
                "SELECT COUNT(t.id) as contador_tickets, 
                 t.id as id_ticket, u.id as user_id, t.fecha_creacion as
                 t_fcreacion, t.hora_creacion as t_hcreacion
                 FROM ticket  as t JOIN usuario AS u ON t.id_usuario = u.id ") 
                or die("Error listando Ticket: ".mysqli_error($conexion));

     $consulta = mysqli_fetch_assoc($pendientes);

    return $consulta['contador_tickets'];
    }

EDITO

If what you want is to consult the tikets that correspond to the id that you pass to the function, is the JOIN necessary? Would not a WHERE be enough?

SELECT COUNT(*) as contador_tickets FROM ticket  as t WHERE t.id_usuario = '$id'
    
answered by 25.04.2018 в 17:52