'Undefined offset' when attempting to retrieve records from the second array

0


I'm a beginner in PHP and I came across a situation where I've been researching and trying alternatives for some time, but without success. I hope you can help me find a solution, because from what I researched in the oracle, I did not find anyone with the same problem and who got a solution. I ask you to signal if I am experiencing a lack in the form of asking the question because I am also new here.

I have a table called 'Processes' that contains a lot of information

I'm setting up a Dashboard that will display some graphics and for that I need to return a query with data separated by Month, Year and Quantity.

The query I use, when executed directly in MySQL, returns the data in 4 records structured like this:

+--------------------+
| ano  |  mes |  qtd |
+------+------+------+
| 2019 |   5  |   1  |
| 2019 |   4  |   2  |
| 2018 |   8  |   6  |
| 2018 |   9  |   46 |
+--------------------+

My PHP code is running like this:

  require_once('db.class.php');
  $objDB = new db();
  $link = $objDB->conecta_mysql();
  mysqli_set_charset($link,"utf8");

  $sql = umSQLmuitoLongo; //está funcionando corretamente

  $resultado = mysqli_query($link, $sql); //executa a query

  if ($resultado) {

     $resultadoFinal = mysqli_fetch_array($resultado, MYSQLI_NUM);

     $mesRecente = $resultadoFinal[0][0];
     $anoRecente = $resultadoFinal[0][1];
     $qtdRecente = $resultadoFinal[0][2];

  }

I need to write the first 12 results into variables, and for each result it will be divided into 3, so the repetition loops, like WHILE, are not useful, since I will need to treat each information in a specific way later

As I'm using MYSQLI_NUM, I tried the following ways to return the strings:

$UltimoAno = $resultadoFinal[0][0];
$UltimoMes = $resultadoFinal[0][1];
$UltimoMesAnoQTD = $resultadoFinal[0][2];

$PenultimoAno = $resultadoFinal[1][0];
$PenultimoMes = $resultadoFinal[1][1];
$PenultimoMesAnoQTD = $resultadoFinal[1][2];

$AntepenultimoAno = $resultadoFinal[2][0];
$AntepenultimoMes = $resultadoFinal[2][1];
$AntepenultimoMesAnoQTD = $resultadoFinal[2][2];

And so it goes. All my attempts went awry, only being able to return the first string by replacing MYSQLI_NUM with MYSQLI_ASSOC. The following continued to go wrong. When I try a print test in the browser, several errors are returned as I try other forms, such as:

Illegal string offset 'month'
Uninitialized string offset Home Sometimes it returns only one character from the string as if it had just picked up the first or second number from the string Home I also used this example of W3schools to help me with the solution to the problem, but also to no avail: link

The question is: What is the best way to collect information from a multidimensional array and define it within variables?

Hugs and thanks for the support!

    
asked by anonymous 21.09.2018 / 07:51

0 answers