0π
When needing to debug something, or developing in general, turning up the error_reporting level and making sure the errors are displayed is a good idea. Add the following two lines to the top of your index.php (or another PHP file which is included everywhere) β
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
Your string assignment for $querymeses
is invalid β
$querymeses = 'SELECT Monthname(fecha_nacimiento) AS mes, count(*) AS Total FROM pacientes GROUP BY mes' WHERE YEAR(fecha_nacimiento) = '2021';
and you should be seeing an error along the lines of β
Parse error: syntax error, unexpected identifier "WHERE" in /tmp/preview on line 5
The WHERE clause needs to be before the GROUP BY and the query is non-sargable due to the YEAR(fecha_nacimiento) = 2021
criterion. I like using heredoc/nowdoc as I think it makes it more readable β
$querymeses = <<<'SQL'
SELECT Monthname(fecha_nacimiento) AS mes, count(*) AS Total
FROM pacientes
WHERE fecha_nacimiento BETWEEN '2021-01-01' AND '2021-12-31'
GROUP BY mes
SQL;
You have $rowmeses=mysqli_fetch_assoc($resmeses);
above your while loop which means you are not using the first row of your result set. And when you echo the labels into your JS the strings need to be quoted.
Putting it all together you end up with β
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$querymeses = <<<'SQL'
SELECT Monthname(fecha_nacimiento) AS mes, count(*) AS Total
FROM pacientes
WHERE fecha_nacimiento BETWEEN '2021-01-01' AND '2021-12-31'
GROUP BY mes
SQL;
$resmeses = mysqli_query($conexion, $querymeses);
$labelMeses = [];
$datosMeses = [];
foreach ($resmeses as $row) {
$labelMeses[] = $row['mes'];
$datosMeses[] = $row['Total'];
}
?>
<script type="text/javascript">
var labeldeMes = ['<?php echo implode("','", $labelMeses); ?>']; //Here I declare the variable labeldeMes
var datosdeMes = [<?php echo implode(',', $datosMeses); ?>]; //Here I declare the variable datosdeMes
</script>