Problem in count php

5

I do not have an error in the connection, but it does not show me correctly the number of pending tickets that I am seeing in the system, try to change the values of tipo_solicitud = 1 && status = 1 to another but it keeps showing 1 .

Code: PHP

function pendientesporrevisar($conexion){    
    $pendientes = (mysqli_query($conexion, "SELECT COUNT(*) FROM ticket
                                             WHERE tipo_solicitud = 1 && status = 1 ")) 
                   or die("Error mostrando  tickets  pendientes: ".mysqli_error($conexion));    
    return $pendientes;
}

Code HTML :

<?php echo $ticket->pendientesporrevisar($conexion)->field_count; ?>
    
asked by Juan Ortiz 17.04.2018 в 21:58
source

4 answers

2

So I did, in my ticket class, add the following function.

function pendientesporrevisar($conexion){

    $pendientes = (mysqli_query($conexion, "SELECT COUNT(*) AS conteo FROM ticket WHERE tipo_solicitud = 1 && status = 1 ")) or die("Error mostrando  tickets  pendientes: ".mysqli_error($conexion));
    $resultados = mysqli_fetch_row($pendientes);
    return $resultados[0];

}

Tested before with the phpmyadmin of my hosting, taking account of the tickets created that have the type of request = 1 and the status = 1.

What I did next was to call them in my html code to show it.

<div align="left" >TICKETS DE ANULACION PENDIENTES POR ATENDER:<?php echo $ticket->pendientesporrevisar($conexion);/* los de anulacion*/ ?>

Sorry if it was very tangled, I'm just starting and I find something difficult to analyze at work (a system made by a very good person in programming). Thank you all for the help and interest.

    
answered by 18.04.2018 / 13:45
source
7

Possibly the problem is in the syntax of the query, you are using the && :

"SELECT COUNT(*) FROM ticket WHERE tipo_solicitud = 1 && status = 1 "

and you should use AND :

"SELECT COUNT(*) FROM ticket WHERE tipo_solicitud = 1 AND status = 1 "

You can read more about the query format MySql here

    
answered by 17.04.2018 в 22:07
3

Possible syntax error:

function pendientesporrevisar($conexion){


$pendientes = (mysqli_query($conexion, "SELECT COUNT(*) FROM ticket WHERE tipo_solicitud = 1 AND status = 1 ")) or die("Error mostrando  tickets  pendientes: ".mysqli_error($conexion));

        return $pendientes;
}

simple replaces & & amp; POR (AND)

    
answered by 17.04.2018 в 22:09
3

Just so you know why your code did not work as expected: for the simple fact that you were using field_count which:

  

Returns the number of columns for the most recent query

In fact, your code was this:

<?php echo $ticket->pendientesporrevisar($conexion)->field_count; ?>

and it always returned 1 because you were recovering the number of columns, not the value of them.

Anyway, it's good to standardize the SQL code, using AND instead of && , even if it works in both ways.

In any case, the problem was in reading the information you were implementing .

It is important to know that there are several ways to obtain the results, using the different fetch methods. Perhaps, for the code to be more readable, it is convenient to use fetch_assoc , I put a example to expand the range of possibilities:

function pendientesporrevisar($conexion){

    $pendientes = (mysqli_query($conexion, "SELECT COUNT(*) conteo FROM ticket WHERE tipo_solicitud = 1 && status = 1 ")) or die("Error mostrando  tickets  pendientes: ".mysqli_error($conexion));
    $resultados = mysqli_fetch_assoc($pendientes);
    $total = ($resultados && $resultados["conteo"]>0) ? $resultados["conteo"] : NULL ;
    return $total;

}

In the function I have also added a supplementary control: using a ternary operator to evaluate the attempt to obtain the data, I assign the variable $total the result obtained or NULL , in case there is any problem ... The value NULL can be replaced by 0 , for a blank string '' or otherwise if you prefer.

    
answered by 18.04.2018 в 17:05