Php query on sqlite3 db returns only the first row

I created a Sqlite3 database with PHP:

$db = new SQLite3('mysqlitedb.db'); $db->exec('CREATE TABLE foo (bar STRING)'); $db->exec("INSERT INTO foo (bar) VALUES ('This is a test')"); $db->exec("INSERT INTO foo (bar) VALUES ('This is another test')"); 

but when I try to get all the lines:

 $result = $db->query('SELECT * FROM foo'); var_dump($result->fetchArray()); 

it returns only the first line in db:

 array(2) { [0]=> string(14) "This is a test" ["bar"]=> string(14) "This is a test" } 

I am not sure why it does not return all rows.

+4
source share
3 answers

You need to iterate over the lines. You will receive only the current line.

 while($row=$result->fetchArray()){ // Do Something with $row print_r($row); } 

PHP manual has a good example on page

+6
source

fetchArray () retrieves only the first row of results. If you want additional lines, make additional calls to fetchArray (possibly in a loop).

+1
source

Request a response when all results are returned in a single array. It seems wasteful to be able to get all the results in one object, split them by array, and then return them to one array.

0
source

Source: https://habr.com/ru/post/1381574/


All Articles