mysqli_fetch_assoc() - PHP

Asked

Viewed 119 times

0

How the function works mysqli_fetch_assoc? Example, if I use it in a function like is below in PHP. What it does?

function buscar_tarefas($conexao){
    $sqlBusca = "SELECT * FROM tarefas";
    $resultado = mysqli_query($conexao, $sqlBusca);
    $tarefas = array();
    while($tarefa = mysqli_fetch_assoc($resultado)){
        $tarefas[] = $tarefa;
    }
    return $tarefas;
}
  • 1

    It’s boring, yes, but we need to ask: did you research documentation? Did you get doubt in any specific part? What you really didn’t understand about the function?

  • my doubt was the return of function, did not understand how it works.

1 answer

4


The function mysqli_fetch_assoc() returns an array with the database results, but the indexes of that array will be represented by the column name, for example:

In a table we have the columns name, email, and phone and the result of a search in this table will be assigned to the variable $res.

Instead of you accessing the data using $res[0], $res[1] and $res[2], you will access using $res['nome'], $res['email'] and $res['telefone'].

Browser other questions tagged

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