How to fix the error of storing a Select in an Array?

Asked

Viewed 21 times

-2

I have this list

$lista_datas = ["2020/02/17","02/04/2020","02/05/2020"];

It works perfectly in my shape system. I recently needed the values in this list to come from a specific column of the database (Xdatas column), which contains other dates. The code doesn’t work. See what I’m trying to do to fetch the values and store in this list.

$sql = "SELECT data FROM basededados";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $lista_datas = $row['XDaTas'];

        }}

How can I resolve this situation to get the database query in the format described at the beginning of this question ?

1 answer

0


If $lista_datas is an array, so you have to use the [] or array_push, thus:

while($row = $result->fetch_assoc()) {
    $lista_datas[] = $row['XDaTas'];
}

Or with the function:

while($row = $result->fetch_assoc()) {
    array_push($lista_datas, $row['XDaTas']);
}

The result is the same in both. This is the basics of the basics, it is very well explained in the documentation (this is the page that teaches the basics of how arrays behave in PHP specifically):

It is no use to program guided kicks if there is a text that already explains, the exact location is in:

Anything you try, take a look first, have a ready and intuitive example.

  • Thanks William, it worked. I’ll have more attention, hugs ...

Browser other questions tagged

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