Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php query on sqlite3 db only returns first row

Tags:

php

sqlite

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.

like image 650
daraeman Avatar asked Nov 16 '11 21:11

daraeman


People also ask

How to LIMIT rows in SQLite?

SQLite Limit: You can limit the number of rows returned by your SQL query, by using the LIMIT clause. For example, LIMIT 10 will give you only 10 rows and ignore all the other rows. In the LIMIT clause, you can select a specific number of rows starting from a specific position using the OFFSET clause.

What does SQLite SELECT return?

SQLite SELECT statement is used to fetch the data from a SQLite database table which returns data in the form of a result table. These result tables are also called result sets.

Does PHP support sqlite3?

PHP provides two SQLite extensions by default since version 5.0. The latest SQLite extension is known as sqlite3 extension that is included in PHP 5.3+. The sqlite3 extension provides an interface for accessing SQLite 3. The sqlite3 includes class interfaces to the SQL commands.


2 Answers

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

like image 73
Mech Avatar answered Sep 22 '22 12:09

Mech


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

like image 23
George Cummins Avatar answered Sep 20 '22 12:09

George Cummins