mysqli_fetch_array without using while

1

Well, I make a query in Mysql like this:

$usuario = $conexao->query("select * from cadastros");

I put a json like this:

// Monta array das tabelas
while ($resultado_tabelas = mysqli_fetch_object($usuario)) {

    // Monta array
    $tabelas[] = array(
        "id" => (int) $resultado_tabelas->id,
        "nome" => $resultado_tabelas->nome
    );
}

Can you do this without using while ? I tried it like this:

mysqli_fetch_array($usuario, MYSQLI_ASSOC),

But I only get 1 result.

    
asked by anonymous 15.09.2017 / 15:28

2 answers

3

If you have mysqlnd installed you can use the mysqli_fetch_all () it is equal to fetchAll() of the PDO ie it returns all query results / rows at once.

$result = $conexao->query("select * from cadastros");
echo json_encode($result->fetch_all(MYSQLI_ASSOC));
    
15.09.2017 / 15:32
1

Without the while has as, no loop has no way, unless you do one by one, which does not make sense, use the foreach.

 <?php
    $arr = mysqli_fetch_array($usuario, MYSQLI_ASSOC);

    foreach($arr as $row){

    }
    
15.09.2017 / 15:32