略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: SQLite3Result

2024-12-22

SQLite3Result 类

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

简介

处理 SQLite 3 扩展返回结果集的类。

类摘要

class SQLite3Result {
/* 方法 */
public columnName(int $column): string|false
public columnType(int $column): int|false
public fetchArray(int $mode = SQLITE3_BOTH): array|false
public finalize(): bool
public numColumns(): int
public reset(): bool
}

目录

add a noteadd a note

User Contributed Notes 6 notes

up
14
jonscully at gmail dot com
12 years ago
Since SQLite3Result::numRows is unavailable, use:

<?php
if ($res->numColumns() && $res->columnType(0) != SQLITE3_NULL) {
   
// have rows
} else {
   
// zero rows
}
?>

Because when there are zero rows:
* SQLite3Result::fetchArray will return '1'
* SQLite3Result::numColumns will return '1'
* Column type for column '0' will be SQLITE3_NULL
up
5
sameers dot public at gmail dot com
8 years ago
According to http://www.php.net/manual/en/sqlite3result.columntype.php, and also per my experience, columnType() doesn't return anything other than SQLITE3_NULL. Consequently, the test suggested in the comment that tries to identify whether a column is NULL is incorrect.

The right thing to do appears to be to test if ($result->fetchArray())[0] == null.
up
5
jan at bootfinder dot co dot uk
7 years ago
I use the following code to get num_rows:

<?php
...
$nrows = 0;
$result->reset();
while (
$result->fetchArray())
   
$nrows++;
$result->reset();
return
$nrows;
...
?>
up
1
atesin () 6m4i1 ! com
1 year ago
in response to jan at bootfinder dot co dot uk (comment #115891) about getting num_rows...

how about (not tested)... ?

<?php // do cleans before and after if needed

for ( $nrows = 0; isarray($result->fetchArray()); ++$nrows );
return
$nrows;

?>
up
0
alan71-at-free-fr
11 years ago
Here's a snippet that might help you to write a fetchObject function that is also missing:

<?php

function fetchObject($sqlite3result, $objectType = NULL) {
   
$array = $sqlite3result->fetchArray();

    if(
is_null($objectType)) {
       
$object = new stdClass();
    } else {
       
// does not call this class' constructor
       
$object = unserialize(sprintf('O:%d:"%s":0:{}', strlen($objectType), $objectType));
    }
   
   
$reflector = new ReflectionObject($object);
    for(
$i = 0; $i < $sqlite3result->numColumns(); $i++) {
       
$name = $sqlite3result->columnName($i);
       
$value = $array[$name];
       
        try {
           
$attribute = $reflector->getProperty($name);
           
           
$attribute->setAccessible(TRUE);
           
$attribute->setValue($object, $value);
        } catch (
ReflectionException $e) {
           
$object->$name = $value;
        }
    }
   
    return
$object;
}

?>

Heavily inspired of Bergmann's Object Freezer :
https://github.com/sebastianbergmann/php-object-freezer/blob/master/Object/Freezer.php
up
-15
claudiu at virtuamagic dot com
12 years ago
fetchArray() will return bool(false) in case of 0 rows.

官方地址:https://www.php.net/manual/en/class.sqlite3result.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3