SQLite3Result::fetchArray

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

SQLite3Result::fetchArray β€” Π’ΠΈΠ±ΠΈΡ€Π°Ρ” Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΡƒΡŽΡ‡ΠΈΠΉ рядок Ρƒ Ρ„ΠΎΡ€ΠΌΡ– масиву Π· асоціативними Π°Π±ΠΎ числовими індСксами, Ρ‡ΠΈ ΠΎΠ±ΠΈΠ΄Π²Π° Π²Π°Ρ€Ρ–Π°Π½Ρ‚Π°.

Опис

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

Π’ΠΈΠ±ΠΈΡ€Π°Ρ” Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΡƒΡŽΡ‡ΠΈΠΉ рядок Ρƒ Ρ„ΠΎΡ€ΠΌΡ– масиву Π· асоціативними Π°Π±ΠΎ числовими індСксами, Ρ‡ΠΈ ΠΎΠ±ΠΈΠ΄Π²Π° Π²Π°Ρ€Ρ–Π°Π½Ρ‚Π°. ΠŸΠΎΡ‡Π°Ρ‚ΠΊΠΎΠ²ΠΎ, Π²ΠΈΠ±ΠΈΡ€Π°ΡŽΡ‚ΡŒΡΡ ΠΎΠ±ΠΈΠ΄Π²Π° Π²Π°Ρ€Ρ–Π°Π½Ρ‚Π°

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΈ

mode

Π’ΠΈΠ·Π½Π°Ρ‡Π°Ρ” як наступний рядок Π±ΡƒΠ΄Π΅ ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ‚ΠΈΡΡŒ ΠΏΡ€ΠΈ Π²ΠΈΠΊΠ»ΠΈΠΊΡƒ. Π¦Π΅ значСння ΠΏΠΎΠ²ΠΈΠ½Π½ΠΎ Π±ΡƒΡ‚ΠΈ ΠΎΠ΄Π½ΠΈΠΌ Π· наступних: SQLITE3_ASSOC, SQLITE3_NUM, Π°Π±ΠΎ SQLITE3_BOTH.

  • SQLITE3_ASSOC: ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ” масив, індСксований ΠΏΠΎ Π½Π°Π·Π²Ρ– ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ, Ρƒ Π²Ρ–Π΄ΠΏΠΎΠ²Ρ–Π΄Π½ΠΈΠΉ Π½Π°Π±Ρ–Ρ€ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ–Π²

  • SQLITE3_NUM: ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ” масив, індСксований ΠΏΠΎ Π½ΠΎΠΌΠ΅Ρ€Ρƒ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ, Ρƒ Π²Ρ–Π΄ΠΏΠΎΠ²Ρ–Π΄Π½ΠΈΠΉ Π½Π°Π±Ρ–Ρ€ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ–Π², ΠΏΠΎΡ‡ΠΈΠ½Π°ΡŽΡ‡ΠΈ Π· ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ 0

  • SQLITE3_BOTH: ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ” масив, індСксований ΠΏΠΎ Π½Π°Π·Π²Ρ– ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ Ρ‚Π° Ρ—Ρ— Π½ΠΎΠΌΠ΅Ρ€Ρƒ, Ρƒ Π²Ρ–Π΄ΠΏΠΎΠ²Ρ–Π΄Π½ΠΈΠΉ Π½Π°Π±Ρ–Ρ€ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ–Π², ΠΏΠΎΡ‡ΠΈΠ½Π°ΡŽΡ‡ΠΈ Π· ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ 0

ЗначСння, Ρ‰ΠΎ ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°ΡŽΡ‚ΡŒΡΡ

Π’ΠΈΠ±ΠΈΡ€Π°Ρ” Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΡƒΡŽΡ‡ΠΈΠΉ рядок Ρƒ Ρ„ΠΎΡ€ΠΌΡ– масиву Π· асоціативними Π°Π±ΠΎ числовими індСксами, Ρ‡ΠΈ ΠΎΠ±ΠΈΠ΄Π²Π° Π²Π°Ρ€Ρ–Π°Π½Ρ‚Π°. Π’Π°ΠΊΠΎΠΆ Π±ΡƒΠ΄Π΅ ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ‚ΠΈ false якщо Π±Ρ–Π»ΡŒΡˆΠ΅ рядків Π½Π΅ΠΌΠ°Ρ”.

οΌ‹add a note

User Contributed Notes 4 notes

up
19
Jason ΒΆ
11 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:

<?php 
        $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.
up
11
paule-panke at example dot com ΒΆ
9 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.
up
3
alan at synergymx dot com ΒΆ
15 years ago
To loop through a record set: 

<?php
        $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);
?>
up
1
ghaith at cubicf dot net ΒΆ
9 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"}
]