SQLite3Result クラス
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
はじめに
SQLite 3 拡張モジュールの結果セットを扱うクラスです。
クラス概要
目次
- SQLite3Result::columnName — n 番目のカラムの名前を返す
- SQLite3Result::columnType — n 番目のカラムの型を返す
- SQLite3Result::__construct — SQLite3Result を構築する
- SQLite3Result::fetchArray — 結果の行を、連想配列あるいは数値添字配列あるいはその両方で取得する
- SQLite3Result::finalize — 結果セットを閉じる
- SQLite3Result::numColumns — 結果セットのカラム数を返す
- SQLite3Result::reset — 結果セットを最初の行に戻す
+add a note
User Contributed Notes 5 notes
jonscully at gmail dot com ¶
15 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
sameers dot public at gmail dot com ¶
10 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.
jan at bootfinder dot co dot uk ¶
10 years ago
I use the following code to get num_rows:
<?php
...
$nrows = 0;
$result->reset();
while ($result->fetchArray())
$nrows++;
$result->reset();
return $nrows;
...
?>
atesin () 6m4i1 ! com ¶
3 years 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;
?>
alan71-at-free-fr ¶
13 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
↑ and ↓ to navigate •
Enter to select •
Esc to close
Press Enter without
selection to search using Google