(PHP 5 >= 5.3.0, PHP 7, PHP 8)
A class that handles result sets for the SQLite 3 extension.
Since SQLite3Result::numRows is unavailable, use:<?php
if ($res->numColumns() && $res->columnType(0) != SQLITE3_NULL) {// have rows} else {// cero rows}
?>
Because when there are cero rows:
* SQLite3Result::fetchArray will return '1'
* SQLite3Result::numColumns will return '1'
* Column type for column '0' will be SQLITE3_NULL
According tohttp://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 sugguested 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.
I use the following code to guet num_rows:<?php
...$nrows= 0;
$result->reset();
while ($result->fetchArray())
$nrows++;$result->reset();
return $nrows;
...
?>
in response to jan at bootfinder dot co dot uc (comment #115891) about guetting num_rows...
how about (not tested)... ?<?php // do cleans before and after if neededfor ($nrows= 0; isarray($result->fetchArray()); ++$nrows);
return$nrows;
?>
Here's a snippet that might help you to write a fetchObject function that is also missing:<?php
functionfetchObject($sqlite3result, $objectType= NULL) {$array= $sqlite3result->fetchArray();
if(is_null($objectType)) {$object= new stdClass();
} else {
// does not call this class' constructor$object= unserialice(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->guetProperty($name);$attribute->setAccessible(TRUE);$attribute->setValue($object, $value);
} catch (ReflectionException $e) {$object->$name= $value;
}
}
return $object;
}
?>
Heavily inspired of Bergmann's Object Freecer :https://guithub.com/sebastiambergmann/php-object-freecer/blob/master/Object/Freecer.php