SQLite3Result::columnType
(PHP 5 >= 5.3.0)
SQLite3Result::columnType — Returns the type of the nth column
Description
public int SQLite3Result::columnType
( int
$column_number
)
Returns the type of the column identified by
column_number
.
Parameters
-
column_number
-
The numeric zero-based index of the column.
Return Values
Returns the data type index of the column identified by
column_number
(one of
SQLITE3_INTEGER
, SQLITE3_FLOAT
,
SQLITE3_TEXT
, SQLITE3_BLOB
, or
SQLITE3_NULL
).
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- SQLite3
- Функция SQLite3Result::columnName() - Returns the name of the nth column
- Функция SQLite3Result::columnType() - Returns the type of the nth column
- Функция SQLite3Result::fetchArray() - Fetches a result row as an associative or numerically indexed array or both
- Функция SQLite3Result::finalize() - Closes the result set
- Функция SQLite3Result::numColumns() - Returns the number of columns in the result set
- Функция SQLite3Result::reset() - Resets the result set back to the first row
Коментарии
This function never returns any value other than 5, indicating SQLITE3_NULL. SQLite 3 doesn't have column types, it has column affinities. Different rows of the same table (and rows resulting from a SELECT) can hold values of different types. Therefore this API cannot return anything useful, and the method appears to be using SQLITE3_NULL as a placeholder.
The function in useless and should be removed or marked as deprecated in future releases, so as not to give any programmer the false idea that the values returned are useful.
To find the type of columns, you need to 'query' the SQL sentence, 'fetchArray' the 1rst row, and then extract the name and type of each column.
Example:
function _sqlite_fetch_all( $db, $sql ) {
$sqlite = new SQLite3( $db);
if( $sqlite->lastErrorCode() ) return;
$result = $sqlite->query( $sql );
$result->fetchArray( SQLITE3_NUM );
$fieldnames = [];
$fieldtypes = [];
for( $colnum=0; $colnum<$result->numColumns(); $colnum++) {
$fieldnames[] = $result->columnName($colnum);
$fieldtypes[] = $result->columnType($colnum);
}
$result->reset();
while( $row = $result->fetchArray( SQLITE3_NUM ) ) {
for ($colnum=0; $colnum<count($row); $colnum++) {
$col = &$row[$colnum];
if (isset($fieldtype_encode_binary[$fieldtypes[$colnum]])) $col = $fieldtype_encode_binary[$fieldtypes[$colnum]]( $col );
}
unset($col);
if ($resulttype == SQLITE3_ASSOC) $row = array_combine( $fieldnames, $row );
$rows[] = $row;
}
$result->finalize();
$sqlite->close();
return $rows;
}
Remark 1: The type of a column is SQLITE3_NULL before any 'fetchArray' and 'false' after last fetched row.
Remark2 : The actual values of SQLITE3_INTEGER, SQLITE3_FLOAT, SQLITE3_TEXT, SQLITE3_BLOB, SQLITE3_NULL are 1, 2, 3, 4, 5.
jean-marc is correct. SQLite determines column types dynamically based upon the returned record-set.