Problem counting records with rowCount ()

0

I need to count a number of records in a table that meet two conditions, the problem is that you are only telling me one record. This is my function:

public function NewClients(){

        $fecha_inicio = date('Y-m-01');
        $fecha_fin = date('Y-m-t');

        $sql = $this->pdo->prepare("SELECT COUNT(id_nivel),date_register FROM usuarios
                                    WHERE id_nivel = 2 AND date_register
                                    BETWEEN '$fecha_inicio' AND '$fecha_fin'");
        $sql->execute();
        echo $sql->rowCount();
    }

I am printing that there is only one record, but in my table there are two records that meet that condition.

    
asked by Alejo Mendoza 16.02.2018 в 17:13
source

1 answer

1

Returns an object.

public function NewClients($fecha_inicio, $fecha_fin){

    $fecha_inicio = date('Y-m-01');
    $fecha_fin = date('Y-m-t');

    $sql = ("SELECT COUNT(id_nivel),date_register FROM usuarios
                                WHERE id_nivel = 2 AND date_register
                                BETWEEN '$fecha_inicio' AND '$fecha_fin'");

    $sentencia = $this->pdo->prepare($sql);
    //$sentencia->bindParam(':fecha_inicio', $fecha_inicio);
    //$sentencia->bindParam(':fecha_fin', $fecha_fin);

    $respuesta = $sentencia->execute();
    print $sentencia->fetch("0")[0];
}
    
answered by 16.02.2018 / 18:07
source