Erro when counting table fields with conditioned SELECT

1

Greetings. What I'm trying to do is the following. I need to count and show how many students have the payment date on the current day, to show it on an alert that says There are 4 students who must pay today. And I also want to list the names of students with today's payment date. I've done the query but apparently he's not telling me the records. The tables I have related as follows:

The tables are related as can be seen. The query that I have made is the following:

 <?php 
 include '../lib/conexion.php';
 date_default_timezone_set('America/Caracas');
 setlocale(LC_ALL, "es_ES");
 $hoy = date('d-m-Y');

 $query = current($mysqli->query("SELECT COUNT(pagos_estudiantes.id), 
 students.names FROM students INNER JOIN inscritos ON 
 inscritos.id_student = students.id_students INNER JOIN pagos_estudiantes 
 ON pagos_estudiantes.id_inscripcion = inscritos.id WHERE 
 pagos_estudiantes.fecha_a_pagar = $hoy")->fetch_assoc());

  if($query){
    $alert = $query;
    $result = "Hay " . $query  . "que deben cancelar hoy";
   }

    echo $alert;
    echo $result;

  ?>

That's my query but as I said what it shows is a 0 in the alert, I mean it's not counting the records. What I need is to show the number of registrations with date_to_pay = today and the names of those students

    
asked by Alejo Mendoza 03.01.2018 в 22:44
source

2 answers

4

The query

You can create a query that will throw you a single row with two columns, in one you will have the count and in another all the names of the students with a separator. Then, you would do explode of the second column to present a list of names.

The query would be more or less like this:

SELECT 
    COUNT(pagos_estudiantes.id) total, 
    GROUP_CONCAT (students.names SEPARATOR '|') nombres 
FROM students 
INNER JOIN inscritos ON inscritos.id_student = students.id_students 
INNER JOIN pagos_estudiantes ON pagos_estudiantes.id_inscripcion = inscritos.id 
WHERE pagos_estudiantes.fecha_a_pagar = ? 
GROUP BY students.id_students

The PHP code

As I said in comments, your PHP code is strange , and it's not the usual way the Manual teaches (or common practice) to read our results.

Also, your query has to be sanitized, because it is vulnerable to SQL injection. To solve this vulnerability, we will modify your code considerably, applying the existing methods to do so. That is, change the value $hoy that you previously passed directly in the SQL statement, by a placeholder ? , and the value itself pass it apart, using the method bind_param .

This is the proposal:

<?php 
    include '../lib/conexion.php';
    date_default_timezone_set('America/Caracas');
    setlocale(LC_ALL, "es_ES");
    $hoy = date('d-m-Y');

    /*El signo ? usado es para preparar la consulta*/
    $query= 
         "SELECT 
            COUNT(pagos_estudiantes.id) total, 
            GROUP_CONCAT (students.names SEPARATOR '|') nombres 
        FROM students 
        INNER JOIN inscritos ON inscritos.id_student = students.id_students 
        INNER JOIN pagos_estudiantes ON pagos_estudiantes.id_inscripcion = inscritos.id 
        WHERE pagos_estudiantes.fecha_a_pagar = ? 
        GROUP BY students.id_students";

    /*Preparamos y pasamos los valores aparte para evitar la inyección de código*/      
    $stmt = prepare($query);
    $stmt->bind_param("s", $hoy); //Aquí se envía el valor, separado de la consulta en sí
    $stmt->bind_result($total, $nombres);
    $stmt->store_result();

    /*Verificamos si hubo resultados*/
    if ($stmt->num_rows > 0){

        while ($stmt->fetch()) {
            $intTotal=$total;
            $arrNombres=explode("|",$nombres);
        }

        echo "Hay " . $intTotal  . " que deben cancelar hoy";

        /*Mostramos una lista con los nombres*/
        echo "<ul>";
        foreach ($arrNombres as $nombre){
            echo "<li>".$nombre."</li>";
        }
        echo "</ul>";

    }else{
        echo "No se encontraron datos";    
    }
    /*Cerrar recursos*/
    $stmt->free_result();
    $stmt->close();
?>
    
answered by 04.01.2018 / 02:06
source
0

Since you need to show the names and other data, do not use the count do the query and then you get the number of rows with num_rows

The consultation can be something like that, and adjust it to your needs.

SELECT *
FROM students 
INNER JOIN inscritos 
ON inscritos.id_student = students.id_students 
INNER JOIN pagos_estudiantes 
ON pagos_estudiantes.id_inscripcion = inscritos.id 
WHERE 
pagos_estudiantes.fecha_a_pagar = $hoy

Then

$resultado = $mysqli->query('CONSULTA');

echo 'Número de registros: '.$resultado->num_rows;

while ($fila = $resultado->fetch_assoc()) {
    echo $fila["names"].', '.$fila["fecha_a_pagar"];
 }

How they have commented to you is a terrible idea to use text for date fields.

    
answered by 04.01.2018 в 01:40