I'm trying convert data from a SQLite3 db to a JSON array by using PHP. I'm getting close, but I can't seem to get it right.
This is the error I'm getting: PHP Warning: PDOStatement::fetchAll() expects parameter 1 to be long, object given in...
Thanks!
<?php
$db = new PDO('sqlite:example.db');
$result = $db->query('SELECT * FROM test');
$json = array();
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($data = $result->fetchall($result)){
$x = $data['Time'];
$y = $data['Temperature'];
$json[] = array( (int($x)), (int($y)) );
}
?>
$result->setFetchMode(PDO::FETCH_ASSOC), and do this on the while line:while ($data = $result->fetchAll(PDO::FETCH_ASSOC))fetchAllin a loop, it returns all the results at once as a 2-dimensional array.$result->fetch(), not$result->fetchAll(). Alternatively, you could call$data = $result->fetchAll()first, then turn the while loop into a foreach loop.