here's the PHP/PDO.
try {
$query = 'SELECT Date,Close FROM DY_AAPL LIMIT 5';
$sth = $db->prepare($query);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)){
$result_array=array();
$result_array[]=$row['Close'];
/* $num = $row['Close']; */
echo json_encode($result_array);
}
}catch (PDOException $e){
echo 'ERROR ' . $e->getMessage();
}
When I attempt to access the array using javascript, it's only outputting the last value in the array. Any pointers?
<script type="text/javascript">
var myjson = JSON.parse('<?php echo json_encode($result_array); ?>');
document.write(myjson);
</script>
I thought it might have something to do with 'JSON.parse,' but I'm not sure. Thanks in advance!
JSON.parse()like that. You can just drop the JSON directly into the JavaScript source as the value of the "myjson" variable.