title: PHP outputs NULL when encoding an array to JSON
body: <p>I have a PHP array that I want to encode to JSON so I can later pass it in my JavaScript file.</p>
  
  <p>When I tried that, the JavaScript outputted: </p>
  
  <blockquote>
    <p>"SyntaxError: Unexpected token N"</p>
  </blockquote>
  
  <p>I then tried to echo this JSON string in PHP and it outputted:</p>
  
  <pre><code>NULL
  {
      "cg": "3",
      "cb": 0,
      "cc": "0%",
      "ci": "0",
      "wm": 0,
      "tl": "120"
  }
  </code></pre>
  
  <p>Why is there a NULL?
  Here's my code:</p>
  
  <pre><code>$table = array(
  "cg" =&gt; $v1,
  "cb" =&gt; $v2,
  "cc" =&gt; $v3,
  "ci" =&gt; $v4,
  "wm" =&gt; $v5,
  "tl" =&gt; $v6
  );
  
  echo json_encode($table);
  </code></pre>
  
  <p><strong>Edit to address the comments:</strong></p>
  
  <p>In the database $v1 is varchar(255) and $v6 is int(11)</p>
  
  <p>The value is retrieved like this:</p>
  
  <pre><code>function fetchinfo($rowname,$tablename,$finder,$findervalue) {
      if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
      else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
      $row = mysql_fetch_assoc($result);
      return $row[$rowname];
  }
  </code></pre>
  
  <p>However this is not always the case, sometimes the value can be a simple string:
  $v6 = "120";</p>