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.
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.
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.
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.
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
fetchArray() fetches only the first row of results. If you want to fetch additional rows, make additional calls to fetchArray (perhaps in a loop).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With