While working with SQLite using its object-oriented mode, I found need to display a column/field name without knowing what it was in advance. I couldn't find any examples on the Internet, just this document. So, for anyone who happens to need to do this, here's an example.
<?php
$db = "db/database.sqlite";
// create new database (OO interface)
$dbo = new SQLiteDatabase("$db");
// create table foo and insert sample data
$dbo->query("
CREATE TABLE foo(id INTEGER PRIMARY KEY, name CHAR(255));
INSERT INTO foo (name) VALUES('Ilia1');
INSERT INTO foo (name) VALUES('Ilia2');
INSERT INTO foo (name) VALUES('Ilia3');
");
$query = "SELECT * FROM foo;";
$result = $dbo->query($query) or die("Error in query");
echo "
<table border='1' cellpadding='10'>
<tr>
<td>".$result->fieldName(0)."</td>
<td>".$result->fieldName(1)."</td>
</tr>";
// iterate through the retrieved rows
while ($result->valid()) {
// fetch current row
$row = $result->current();
echo "
<tr>
<td>".$row[0]."</td>
<td>".$row[1]."</td>
</tr>";
// proceed to next row
$result->next();
}
echo "</table>";
?>
sqlite_field_name
SQLiteResult->fieldName
SQLiteUnbuffered->fieldName
(PHP 5, PECL sqlite >= 1.0.0)
sqlite_field_name -- SQLiteResult->fieldName -- SQLiteUnbuffered->fieldName — Returns the name of a particular field
Description
Object oriented style (method):
Given the ordinal column number, field_index , sqlite_field_name() returns the name of that field in the result set result .
Parameters
- result
-
The SQLite result resource. This parameter is not required when using the object-oriented method.
- field_index
-
The ordinal column number in the result set.
Return Values
Returns the name of a field in an SQLite result set, given the ordinal column number; FALSE on error.
The column names returned by SQLITE_ASSOC and SQLITE_BOTH will be case-folded according to the value of the sqlite.assoc_case configuration option.
sqlite_field_name
22-Jun-2007 08:03
