php query on sqlite3 db only returns first row

10,462

Solution 1

You need to iterate over the rows. You'll only get the current row.

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

The PHP Manual has a good example on the page

Solution 2

fetchArray() fetches only the first row of results. If you want to fetch additional rows, make additional calls to fetchArray (perhaps in a loop).

Share:
10,462
daraeman
Author by

daraeman

This sentence contains all you ever need to know About Me.

Updated on July 12, 2022

Comments

  • daraeman
    daraeman almost 2 years

    I created an 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 rows:

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

    it only returns the first row in the db:

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

    I'm not sure why it isn't returning all the rows.