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-1.0.3)
sqlite_field_name -- SQLiteResult->fieldName -- SQLiteUnbuffered->fieldName — Obtiene el nombre de un campo
Descripción
Método que sigue el estilo orientado a objetos:
A partir del número de columna (indice_campo ) sqlite_field_name() devuelve el nombre de ese campo dentro del resultado indicado con manejador_resultado .
Lista de parámetros
- manejador_resultado
-
El identificador del resultado de SQLite. Este parámetro no es obligatorio cuando se emplea el método orientado a objetos.
- indice_campo
-
El número de columna dentro del resultado.
Valores retornados
Devuelve el nombre del campo del resultado de SQLite a partir de su número de columna o FALSE si se ha producido un 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 10:03
