Add text to an array and print with json_enconde from PHP

Asked

Viewed 22 times

0

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

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

How I would add an item to 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;
  • 1

    In this example, it seems that you are already returning the full line within the array (with all values defined in the select of sql). How exactly is the result being shown?

1 answer

0

Try this way, so you choose what and 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']
    );
}
  • nothing appears like that you said, very strange

Browser other questions tagged

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