PHP Conference Japan 2024

SQLite3Result 类

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

简介

一个处理 SQLite 3 扩展结果集的类。

类概要

class SQLite3Result {
/* 方法 */
private __construct()
public columnName(int $column): string|false
public columnType(int $column): int|false
public finalize(): true
public numColumns(): int
public reset(): bool
}

目录

添加注释

用户贡献的注释 5 个注释

15
jonscully at gmail dot com
15 年前
由于 SQLite3Result::numRows 不可用,请使用

<?php
if ($res->numColumns() && $res->columnType(0) != SQLITE3_NULL) {
// 有行
} else {
// 零行
}
?>

因为当没有行时
* SQLite3Result::fetchArray 将返回 '1'
* SQLite3Result::numColumns 将返回 '1'
* 列 0 的类型将为 SQLITE3_NULL
4
sameers dot public at gmail dot com
10 年前
根据 https://php.net/manual/en/sqlite3result.columntype.php, 以及我的经验,columnType() 除了 SQLITE3_NULL 之外不返回任何其他内容。因此,注释中建议用于识别列是否为 NULL 的测试是不正确的。

正确的方法似乎是测试 if ($result->fetchArray())[0] == null。
3
jan at bootfinder dot co dot uk
10 年前
我使用以下代码获取 num_rows

<?php
...
$nrows = 0;
$result->reset();
while (
$result->fetchArray())
$nrows++;
$result->reset();
return
$nrows;
...
?>
2
atesin () 6m4i1 ! com
3 年前
针对 jan at bootfinder dot co dot uk(注释 #115891)关于获取 num_rows 的内容...

如何(未测试)...?

<?php // 如果需要,请在前后进行清理

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

?>
0
alan71-at-free-fr
13 年前
以下代码片段可能有助于您编写也缺少的 fetchObject 函数

<?php

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

if(
is_null($objectType)) {
$object = new stdClass();
} else {
// 不调用此类的构造函数
$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;
}

?>

大量借鉴了 Bergmann 的对象冻结器
https://github.com/sebastianbergmann/php-object-freezer/blob/master/Object/Freezer.php
To Top