6

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.

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

4 Comments

To follow on to why you must do this... imagine you had a database with several million rows and each row was many KB large... consider how much memory you would need to load the entire table at once.
@mah that would be funny to see a server load all that into memory!
@MechSoftware thanks that worked, first time diving into sqlite3.
Thank you. Please "Accept" the answer (check mark outline) if it works for you. Best of luck!
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).

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.