Php sqlite select all rows

How can I select all rows using SQLite3::open? The following code only outputs one row.

open('db_backups/database_123456789.db');
        }
    }

    $db = new MyDB();

    $result = $db->query('SELECT * FROM myBookmarks');
    var_dump($result->fetchArray());
    // ONLY DUMPS ONE RESULT
    ?>

I got my starting code here. http://www.php.net/manual/en/sqlite3.open.php

  • php
  • sqlite

asked Apr 12, 2016 at 3:09

Papa De BeauPapa De Beau

3,57618 gold badges78 silver badges134 bronze badges

5

  • Also, the query isn't run from open method. It's run from the query method. Maybe you need to read all of the documentation to get a better understanding before trying to implement code you don't understand.

    Apr 12, 2016 at 3:14

  • I get an error when I try this code: Fatal error: Call to undefined function sqlite_open()

    Apr 12, 2016 at 3:16

  • I found the answer. I posted it. Thanks for your help.

    Apr 12, 2016 at 3:18

  • Soooo, reading the documentation further helped?

    Apr 12, 2016 at 6:25

1 Answer

answered Apr 12, 2016 at 3:19

Papa De BeauPapa De Beau

3,57618 gold badges78 silver badges134 bronze badges

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

SQLite3Result::fetchArray Fetches a result row as an associative or numerically indexed array or both

Description

public SQLite3Result::fetchArray(int $mode = SQLITE3_BOTH): array|false

Parameters

mode

Controls how the next row will be returned to the caller. This value must be one of either SQLITE3_ASSOC, SQLITE3_NUM, or SQLITE3_BOTH.

  • SQLITE3_ASSOC: returns an array indexed by column name as returned in the corresponding result set

  • SQLITE3_NUM: returns an array indexed by column number as returned in the corresponding result set, starting at column 0

  • SQLITE3_BOTH: returns an array indexed by both column name and number as returned in the corresponding result set, starting at column 0

Return Values

Returns a result row as an associatively or numerically indexed array or both. Alternately will return false if there are no more rows.

The types of the values of the returned array are mapped from SQLite3 types as follows: integers are mapped to int if they fit into the range PHP_INT_MIN..PHP_INT_MAX, and to string otherwise. Floats are mapped to float, NULL values are mapped to null, and strings and blobs are mapped to string.

paule-panke at example dot com

5 years ago

Check with SQLite3Result::numColumns() for an empty result before calling SQLite3Result::fetchArray().

In contrast to the documentation SQLite3::query() always returns a SQLite3Result instance, not only for queries returning rows (SELECT, EXPLAIN). Each time SQLite3Result::fetchArray() is called on a result from a result-less query internally the query is executed again, which will most probably break your application.
For a framwork or API it's not possible to know in before whether or not a query will return rows (SQLite3 supports multi-statement queries). Therefore the argument "Don't execute query('CREATE ...')" is not valid.

Jason

7 years ago

Would just like to point out for clarification that each call to fetchArray() returns the next result from SQLite3Result in an array, until there are no more results, whereupon the next fetchArray() call will return false.

HOWEVER an additional call of fetchArray() at this point will reset back to the beginning of the result set and once again return the first result. This does not seem to explicitly documented, and caused me my own fair share of headaches for a while until I figured it out.

For example:

        $returned_set = $database->query("select query or whatever");//Lets say the query returned 3 results
        //Normally the following while loop would run 3 times then, as $result wouldn't be false until the fourth call to fetchArray()
       
while($result = $returned_set->fetchArray()) {
               
//HOWEVER HAVING AN ADDITIONAL CALL IN THE LOOP WILL CAUSE THE LOOP TO RUN AGAIN
               
$returned_set->fetchArray();
        }
?>

Basically, in the above code fetchArray will return:
1st call | 1st result from $returned_set (fetchArray() call from while condition)
2nd call | 2nd result  (fetchArray() call from while block)
3rd call | 3rd result  (fetchArray() call from while condition)
4th call |FALSE  (fetchArray() call from while block)
5th call | 1st result  (fetchArray() call from while condition)
....

This will cause (at least in this case) the while loop to run infinitely.

ghaith at cubicf dot net

5 years ago

// Open a new sqlite3 DB

$db = new SQLite3('./DB_EHLH.db');

// select all information from table "algorithm"

$results= $db->query("select * from algorithm");

//Create array to keep all results
$data= array();

// Fetch Associated Array (1 for SQLITE3_ASSOC)
while ($res= $results->fetchArray(1))
{
//insert row into array
array_push($data, $res);

}

//you can return a JSON array
echo json_encode($data);

//the output is a s follows
[
{"id":1,"algorithm":"GA"},
{"id":2,"algorithm":"PSO"},
{"id":3,"algorithm":"IWO"},
{"id":4,"algorithm":"OIWO"}
]

alan at synergymx dot com

12 years ago

To loop through a record set:

        $db = new SQLite3('auth.sqlite'); $sql = "SELECT user_id, username, opt_status FROM tbl_user"; $result = $db->query($sql);//->fetchArray(SQLITE3_ASSOC); $row = array(); $i = 0;

         while(

$res = $result->fetchArray(SQLITE3_ASSOC)){

             if(!isset(

$res['user_id'])) continue; $row[$i]['user_id'] = $res['user_id'];
             
$row[$i]['username'] = $res['username'];
             
$row[$i]['opt_status'] = $res['opt_status']; $i++;

          }

print_r($row);
?>

How to fetch data from SQLite database in PHP?

To query data from a table, you use the following steps:.
Connect to the SQLite database using the PDO object..
Use the query() method of the PDO object to execute the SELECT statement. ... .
Loop through the result set using the fetch() method of the PDOStatement object and process each row individually..

How to use SQLite3 in PHP?

In this chapter, you will learn how to use SQLite in PHP programs..
Installation. SQLite3 extension is enabled by default as of PHP 5.3. ... .
PHP Interface APIs. ... .
Connect to Database. ... .
Create a Table. ... .
INSERT Operation. ... .
SELECT Operation. ... .
UPDATE Operation. ... .
DELETE Operation..

How to check SQLite version in PHP?

PHP SQLite3 version example php $ver = SQLite3::version(); echo $ver['versionString'] . "\n"; echo $ver['versionNumber'] . "\n"; var_dump($ver); The SQLite3::version returns the version of the SQLite database.

How to CREATE SQLite database in PHP?

//create or open (if exists) the database $database = new SQLite3('myDatabase. sqlite'); If you have named a database that doesn't exist it should get created. Have you checked the permissions of the folder where you're trying to create the new database?