All data of a query in an array

Asked

Viewed 50 times

-2

I need all the data from a select to be in an array format. All the ways I’ve tried return only the last data from the table. How should I proceed ?

$sql = "SELECT * FROM Termos";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_array()) {

   $palavras = "array(" . $row['Descricao']. ")" ;

    }
}

If in the database had the words LIFE, TEST AND DREAM, I would like a return exactly like this>

$words = array("life", "test", "dream");

1 answer

2


See this solves your problem

    $itens= [];
    $sql = "SELECT nome
            FROM termos";

    $stmt = $this->conexao->prepare($sql);

    $stmt->execute();
    $stmt->bind_result($termo);
    while ($stmt->fetch()) {

        array_push($itens, $termo);
    }
    return $itens;
  • What error is appearing?

  • Solved my problem, thank you very much friend, the mistake was on my part here, perfect!

  • Success, happy to help you

Browser other questions tagged

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