note 115928 added to sqlite3result.columntype
| From: | jean-marc at paratte dot ch | Date: | Wed, 15 Oct 2014 12:49:19 +0000 |
| Subject: | note 115928 added to sqlite3result.columntype | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200654@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 62.2.182.223
Probable Submitter: 212.101.16.207
----
Manual Page -- http://php.net/manual/en/sqlite3result.columntype.php
Edit -- https://master.php.net/note/edit/115928
Del: integrated -- https://master.php.net/note/delete/115928/integrated
Del: useless -- https://master.php.net/note/delete/115928/useless
Del: bad code -- https://master.php.net/note/delete/115928/bad+code
Del: spam -- https://master.php.net/note/delete/115928/spam
Del: non-english -- https://master.php.net/note/delete/115928/non-english
Del: in docs -- https://master.php.net/note/delete/115928/in+docs
Del: other reasons-- https://master.php.net/note/delete/115928
Reject -- https://master.php.net/note/reject/115928
Search -- https://master.php.net/manage/user-notes.php