文字

SQLite3Result::fetchArray

(PHP 5 >= 5.3.0)

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

说明

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

Fetches a result row as an associative or numerically indexed array or both. By default, fetches as both.

参数

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

返回值

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

用户评论:

[#1] bartonphillips at gmail dot com [2015-04-22 22:06:32]

The 'fetchArray' seems to return NULL not false when eof.

<?php
$db 
= new SQLite3('database');
$r $db->query("select name from test where rowid=1");
list(
$name) = $r->fetchArray(SQLITE3_NUM);
var_dump($name);
list(
$name) = $r->fetchArray(SQLITE3_NUM);
echo 
$name === false "FALSE\n" "NOT FALSE\n";
var_dump($name);
?>

The output is:
NOT FALSE
string(11) "Some String"
NULL

While NULL does evaluate to FALSE in '$fname == false' it is NOT false.

[#2] Jason [2014-10-03 22:21:15]

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.

[#3] alan at synergymx dot com [2010-09-14 16:56:28]

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);
?>

上一篇: 下一篇: