Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The particular error you have cited (<em>JSON.parse: unexpected non-whitespace character after JSON data</em>) is when Javascript is decoding it.</p> <p><code>json_encode</code> returns a string from the mixed value argument. <a href="http://php.net/manual/en/function.json-encode.php" rel="nofollow">json_encode Doc</a></p> <p>You would either need to <code>echo</code> out the result or add it to another variable to output later. eg.</p> <pre><code>while ($row = $res-&gt;fetchRow()){ $resGold = $row['gold']; $resSilver = $row['silver']; $resBronze = $row['bronze']; $resGdp = $row['gdp']; $resPopulation = $row['population']; $resCountry = $row['country_name']; $gold_score = ($resGold * $gold_value); $silver_score = ($resSilver * $silver_value); $bronze_score = ($resBronze * $bronze_value); $score_pop = (($gold_score + $silver_score + $bronze_score)/$resPopulation); $score_gdp = (($gold_score + $silver_score + $bronze_score)/$resGdp); if($population == 'true'){ $result = $res-&gt;fetchRow(); $result['score'] = "$score_pop"; echo json_encode($result); } else if($gdp == 'true'){ $result = $res-&gt;fetchRow(); $result['score'] = "$score_gdp"; echo json_encode($result); } } if($population == 'false' &amp;&amp; $gdp == 'false'){ echo "Please select either population or gdp from view.htm"; } </code></pre> <p>If you are trying to encode multiple rows and return them all, you would be better off adding the <code>$result</code> to an array and encoding that outside the <code>while</code> loop as if you don't, your JSON string could look like:</p> <pre><code>{gold:123,silver:456,bronze:789}{gold:987,silver:654,bronze:321} </code></pre> <p>That is not valid JSON as it can only parse one object or array at once. Below is a valid JSON string</p> <pre><code>[{gold:123,silver:456,bronze:789},{gold:987,silver:654,bronze:321}] </code></pre> <p>That is an array representation of your data and will parse into a list of your JSON encoded objects. Here is your code using an array to store the JSON before echoing it.</p> <pre><code>$results = array(); while ($row = $res-&gt;fetchRow()){ $resGold = $row['gold']; $resSilver = $row['silver']; $resBronze = $row['bronze']; $resGdp = $row['gdp']; $resPopulation = $row['population']; $resCountry = $row['country_name']; $gold_score = ($resGold * $gold_value); $silver_score = ($resSilver * $silver_value); $bronze_score = ($resBronze * $bronze_value); $score_pop = (($gold_score + $silver_score + $bronze_score)/$resPopulation); $score_gdp = (($gold_score + $silver_score + $bronze_score)/$resGdp); if($population == 'true'){ $result = $res-&gt;fetchRow(); $result['score'] = "$score_pop"; array_push($results,$result); } else if($gdp == 'true'){ $result = $res-&gt;fetchRow(); $result['score'] = "$score_gdp"; array_push($results,$result); } } if($population == 'false' &amp;&amp; $gdp == 'false'){ echo "Please select either population or gdp from view.htm"; } else { //Note: This is in the 'else' statement as echoing the JSON then that string // will also cause errors as it ends up not being valid JSON anymore echo json_encode($results); } </code></pre> <p>As in my example above you are echoing the string regarding selecting population or GDP, as that isn't JSON encoded and the part above would otherwise be encoded in JSON, you may have parse errors when trying to decode it. If this PHP page is meant to return JSON encoded data and your error message isn't JSON encoded, it is likely to have problems with whatever is getting the value.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload