davidraedev
davidraedev

Reputation: 119

php query on sqlite3 db only returns first row

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.

Upvotes: 6

Views: 11165

Answers (3)

NuclearPeon
NuclearPeon

Reputation: 6049

Requesting an answer where all results are returned in one array. It seems wasteful to be able to get all results in one object, have them split by array entry, then having to put them back into one array.

Upvotes: 0

George Cummins
George Cummins

Reputation: 28906

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

Upvotes: 2

QuantumRob
QuantumRob

Reputation: 2924

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

Upvotes: 9

Related Questions