I do not work fetch_all (MYSQLI_ASSOC)

1

I am a student and in one of my video classes I realize that my code is broken by using the function fetch_all (MYSQLI_ASSOC) verify the syntax of my code again and again until I have done it practically from the beginning and not yet I can solve the problem.

 <?php include_once 'includes/templates/header.php'; ?>

    <section class="seccion contenedor">
          <h2>Calendario de Eventos</h2>

          <?php
              try {
                require_once('includes/funciones/bd_conexion.php');
                $sql = 'SELECT* FROM 'eventos';';
                $resultado = $conn->query($sql);
              } catch (Exception $e) {
                $error = $e->getMessage();
              }
           ?>

           <?php while($eventos = $resultado->fetch_all(MYSQLI_ASSOC) ) { ?>

             <pre><?php var_dump($eventos); ?></pre>

           <?php } ?>

    </section>

That would be the code that should work, when I save the changes and reload the browser only loads the part of the "header.php" and the title "Calendar of Events" and the "footer.php" does not appear. It's like I'm not there. In fact

 <?php include_once 'includes/templates/header.php'; ?>

    <section class="seccion contenedor">
          <h2>Calendario de Eventos</h2>

          <?php
              try {
                require_once('includes/funciones/bd_conexion.php');
                $sql = 'SELECT* FROM 'eventos';';
                $resultado = $conn->query($sql);
              } catch (Exception $e) {
                $error = $e->getMessage();
              }
           ?>

           <?php while($eventos = $resultado->fetch_assoc() ) { ?>

             <pre><?php var_dump($eventos); ?></pre>

           <?php } ?>

    </section>

If I change the function mentioned above by fetch_assoc () it works correctly without problems. The problem is that to continue with the video class I have to use fetch_all (MYSQLI_ASSOC). Otherwise, I do not get the results I should get.

    
asked by Maxii Perez 03.10.2017 в 00:19
source

2 answers

1

When that function takes you out of error, it means that the query is the wrong one because, being wrong, it does not return what you really expect the fetch_all() , in your line of $sql I see that you have one; within string of your query that may be what is breaking test doing it as follows:

$sql = "SELECT * FROM eventos";

Something else, if you are using PDO I understand that the function is as follows fetchAll()

I hope you serve, greetings!

SOLVED

After debugging a error 500 that came out in consola and in network the decision was made to change the server (from mamp to xampp) and this was enough to achieve the expected result since in As for code there was no error

    
answered by 03.10.2017 / 00:23
source
1

Your code may not work for two reasons.

1st reason: Wrong use of the function

The Manual says very clearly the following :

  

Since mysqli_fetch_all() returns all the rows in an array in   One step , can consume more memory than other functions   similar ones such as mysqli_fetch_array() , which only returns one   row each time from the result. Therefore, if needed   to go through a result, a loop that minimizes the impact will be necessary   in performance. For this reason, it is advised that only use    mysqli_fetch_all() in those situations where the result is   have to send to another layer or level to process it.

That is, when you use fetch_all you do not have to read the result in a loop ... you already have the whole result!

Then:

$eventos=mysqli_fetch_all($resultado,MYSQLI_ASSOC);
var_dump($eventos);

All the result is already in $eventos, that's what fetch_all does.

2nd reason: You do not have the driver mysqlnd

If you correct what has been said before and it does not work, keep in mind that the Manual says that fetch_all is

  

available only with    mysqlnd .

If you do not have it installed, it will not work for you.

To get rid of doubts, run this test:

$mysqlnd = function_exists('mysqli_fetch_all'); 

if ($mysqlnd) 
{
    echo 'mysqlnd activado!'; 

}else{ 

    echo 'Lo siento, mysqlnd no está activado';

}

The solution that could be more optimal

Taking into account the above, the most optimal solution would be a code that does not depend on the driver mysqlnd and also does not affect the performance, that is, the code as you have it in the second part of your question, correcting the spelling error of SELECT* :

 
<section class="seccion contenedor">
      <h2>Calendario de Eventos</h2>

      <?php
          try {
            require_once('includes/funciones/bd_conexion.php');
            $sql = 'SELECT * FROM 'eventos';';
            $resultado = $conn->query($sql);
          } catch (Exception $e) {
            $error = $e->getMessage();
          }
       ?>

       <?php while($eventos = $resultado->fetch_assoc() ) { ?>

         <pre><?php var_dump($eventos); ?></pre>

       <?php } ?>

</section>
    
answered by 03.10.2017 в 03:56