mysqli_fetch_array without using while

Asked

Viewed 404 times

1

Good do a query in Mysql so:

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

I ride 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
    );
}

You can do that without using the while? I tried so:

mysqli_fetch_array($usuario, MYSQLI_ASSOC),

But I only get 1 result.

2 answers

3

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

$result = $conexao->query("select * from cadastros");
echo json_encode($result->fetch_all(MYSQLI_ASSOC));

1

Without the while has like, no loop has no way, unless you do one by one, which makes no sense, use the foreach.

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

    foreach($arr as $row){

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.