title: How iterate a fetch_assoc() array?
body: <p>Im a very novice programmer, i work with PHP and a have a doubt/problem with fetch_assoc().</p>
  
  <p>I have this method that return the result of the database consult:</p>
  
  <pre><code>public function getUsuarios() {
      try {
          $resultado = $this-&gt;conexion-&gt;query("SELECT * FROM login_usuarios");
      } catch(Exception $e) {
          throw new Exception("Error al obtener los usuarios de la base de datos. Código de error: ".mysqli_errno);
      } finally {
          if($this-&gt;conexion != null) {
                  $this-&gt;conexion-&gt;close();
          }
      }
      return $resultado;
  }
  </code></pre>
  
  <p>This script iterate and show the data of the database consult's result:</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;Código&lt;/td&gt;
          &lt;td&gt;Nombres&lt;/td&gt;
          &lt;td&gt;Apellidos&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php while ($fila = mysqli_fetch_array($usuarios)) { ?&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;?php print($fila['codUsuario']); ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php print($fila['nombres']); ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php print($fila['apellidos']); ?&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php
      }
      $usuarios-&gt;free();
      ?&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>With this way, all worked fine (but i don't understand what xD, i've see this way in a forum).</p>
  
  <p>But, i've see other way in many sites. With this way the script stand like this:</p>
  
  <pre><code>// first convert the result to a fetch_assoc
  $usuarios = (new Consultas)-&gt;getUsuarios()-&gt;fetch_assoc();
  
    &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;Código&lt;/td&gt;
          &lt;td&gt;Nombres&lt;/td&gt;
          &lt;td&gt;Apellidos&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php foreach ($usuarios as $fila) { ?&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;?php print($fila["codUsuario"]); ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php print($fila["nombres"]); ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php print($fila["apellidos"]); ?&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php
      }
      $usuarios-&gt;free();
      ?&gt;--&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>But with this way i have a <strong>Illegal string offset mysql</strong>. This looks like the foreach iterate on each column and not on each row.</p>
  
  <p>Why the first way works and the second way don't?, How works mysqli_fetch_array($result) and fetch_assoc()?</p>
  
  <p>Thanks for help.</p>