Appending text to an array and printing with json_enconde of PHP

0

I have this code in php that printed the result array of select mysql:

It currently prints the name, date and end time and start of the event.

How would I add an item in the array, such as the "id" of each record?

header('Content-Type: application/json');
        $start = mysqli_real_escape_string($connection,$_GET["start"]);
        $end = mysqli_real_escape_string($connection,$_GET["end"]);

        $result = mysqli_query($connection,"SELECT 'id', 'start' ,'end' ,'title','ativo' FROM  'events' where (date(start) >= '$start' AND date(start) <= '$end' AND ativo='nao'  ) ");
        while($row = mysqli_fetch_assoc($result))
        {

            $events[] = $row; 
        }


        echo json_encode($events); 



        exit;
    
asked by anonymous 30.10.2018 / 19:52

1 answer

0

Try this way, so you choose what is the order you want to appear. I advise you to start using PDO

header('Content-Type: application/json');
$start = mysqli_real_escape_string($connection,$_GET["start"]);
$end = mysqli_real_escape_string($connection,$_GET["end"]);

$result = mysqli_query($connection,"SELECT 'id', 'start' ,'end' ,'title','ativo' FROM  'events' where (date(start) >= '$start' AND date(start) <= '$end' AND ativo='nao'  ) ");
while($row = mysqli_fetch_assoc($result))
{           
    $events[] = array(
        'id' => $row['id'],
        'start' => $row['start'],
        'end' => $row['end']
    );
}
    
01.11.2018 / 18:16