mysqli_fetch_assoc () - PHP

0

How does the mysqli_fetch_assoc function work? Example, if I use it in a function as below in PHP. What does it do?

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;
}
    
asked by anonymous 25.10.2018 / 15:37

1 answer

5

The function mysqli_fetch_assoc() returns an array with the results of the database, but the indexes of this 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 $res variable.

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

25.10.2018 / 15:51