Saturday 15 February 2014

php - How iterate a fetch_assoc() array? -



php - How iterate a fetch_assoc() array? -

im novice programmer, work php , have doubt/problem fetch_assoc().

i have method homecoming result of database consult:

public function getusuarios() { seek { $resultado = $this->conexion->query("select * login_usuarios"); } catch(exception $e) { throw new exception("error al obtener los usuarios de la base of operations de datos. código de error: ".mysqli_errno); } { if($this->conexion != null) { $this->conexion->close(); } } homecoming $resultado; }

this script iterate , show info of database consult's result:

<table> <tr> <td>código</td> <td>nombres</td> <td>apellidos</td> </tr> <?php while ($fila = mysqli_fetch_array($usuarios)) { ?> <tr> <td><?php print($fila['codusuario']); ?></td> <td><?php print($fila['nombres']); ?></td> <td><?php print($fila['apellidos']); ?></td> </tr> <?php } $usuarios->free(); ?> </table>

with way, worked fine (but don't understand xd, i've see way in forum).

but, i've see other way in many sites. way script stand this:

// first convert result fetch_assoc $usuarios = (new consultas)->getusuarios()->fetch_assoc(); <table> <tr> <td>código</td> <td>nombres</td> <td>apellidos</td> </tr> <?php foreach ($usuarios $fila) { ?> <tr> <td><?php print($fila["codusuario"]); ?></td> <td><?php print($fila["nombres"]); ?></td> <td><?php print($fila["apellidos"]); ?></td> </tr> <?php } $usuarios->free(); ?>--> </table>

but way have illegal string offset mysql. looks foreach iterate on each column , not on each row.

why first way works , sec way don't?, how works mysqli_fetch_array($result) , fetch_assoc()?

thanks help.

construction finally closes connection before phone call fetch_assoc().

try comment $this->conexion->close();.

your code iterates through row. mysqli_result::fetch_assoc homecoming 1 row. please, read documentation.

php mysqli

No comments:

Post a Comment