0

Admittedly it has been a while since I have been in the php world, but i'm not sure why I am getting 3 spaces at the start of my json_encode string.

"   [{"id":"1","display_name":"Bob"}]"

This is the php i have:

$stmt = $conn->prepare("SELECT * FROM users");

$queryResult = array();
if ($stmt->execute()) {
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $queryResult[] = $row;
  }
  echo json_encode($queryResult);
}

When I echo print_r($stmt->fetch(PDO::FETCH_ASSOC)) I get the following:

   Array
(
    [id] => 1
    [display_name] => Twitch Dock John
)

The 3 spaces are there as well.. what have I done wrong (expecting an incoming face-palm moment)?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Zze
  • 18,229
  • 13
  • 85
  • 118

1 Answers1

5

You can possibly figure the spot in your codebase where output starts by determining where HTTP headers have been sent:

if (headers_sent($file, $line)) {
    echo "Ouput starts on: $file: $line\n";
}
Álvaro González
  • 142,137
  • 41
  • 261
  • 360