Error counting list with PHP count

1

I have this function that brings me the tickets associated with a user_department of the user that enters the system

function listTicketUnrevisedSupervisor($conexion, $id){

    $consulta = mysqli_query($conexion, "SELECT *,
     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
     WHERE t.status <> '3'
     AND u.id_departamento = ".$id."
     ORDER BY t.id DESC ") 
     or die("Error listando Ticket: ".mysqli_error($conexion));

    return $consulta;
}

I'm trying to do the count of the list but I have errors

function contarlistTicketUnrevisedSupervisor($conexion, $id){

    $consulta = mysqli_query($conexion, "SELECT *, 
     COUNT(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
     WHERE t.status <> '3'
     AND u.id_departamento = ".$id."") 
     or die("Error listando Ticket: ".mysqli_error($conexion));

    return $consulta;
}

"you have an error in your sql syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near * at line 5"

The two parameters & connection and the $ id have already been declared, including the apartment_id whose value I assign it to when entering and bring it to me, what am I doing wrong?

    
asked by Juan Ortiz 24.04.2018 в 15:55
source

2 answers

0

You can change your query for this:

$consulta = mysqli_query($conexion, "SELECT SQL_CALC_FOUND_ROWS *, 
 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
 WHERE t.status <> '3'
 AND u.id_departamento = ".$id."") 
 or die("Error listando Ticket: ".mysqli_error($conexion));

And then after running the query you can run:

SELECT FOUND_ROWS();

This will return the number of rows of the last query.

    
answered by 24.04.2018 в 16:49
0

A possible solution would be to use the row counter (records) of php mysqli_num_rows example:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* verificar la conexión */
if (mysqli_connect_errno()) {
    printf("Conexión fallida: %s\n", mysqli_connect_error());
    exit();
}

if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {

    /* determinar el número de filas del resultado */
    $row_cnt = $result->num_rows;

    printf("Result set has %d rows.\n", $row_cnt);

    /* cerrar el resultset */
    $result->close();
}

/* cerrar la conexión */
$mysqli->close();
?>
    
answered by 24.04.2018 в 17:01