It isnt specified in the above description for this function, but yes this function will not only return the first column, but it will also return the first value (ie from the first row in the result set) of that column. Also the result set pointer is involved here in that if you use sqlite_fetch_single in a loop, it will return each value in the first column sequentially and advance the result set pointer each time.
i.e. the first loop iteration it returns the first value in the first column. the second loop iteration it returns the second value in the first column. the third loop iteration the third value and so on.
$handle = sqlite_open("database_file_name") or die("Could not open database");
//generate query string
$query = "select distinct field1 from database_name";
//execute query
$result = sqlite_query($handle,$query)
//if a row exists
if(sqlite_num_rows($result)>0){
echo "<table cellpadding=\"10\" border=\"1\">";
//check for more rows
while(sqlite_has_more($result)){
//get first field from each row
//print values
$row=sqlite_fetch_single($result);
echo "<tr>";
echo "<td>".$row."</td>";
echo "</tr>";
}
echo "</table>";
}
//close database file
sqlite_close($handle);
sqlite_fetch_single
SQLiteResult->fetchSingle
SQLiteUnbuffered->fetchSingle
(No version information available, might be only in CVS)
SQLiteUnbuffered->fetchSingle — Obtiene la primera columna del resultado en forma de cadena
Descripción
Método que sigue el estilo orientado a objetos:
sqlite_fetch_single() es idéntica a la función sqlite_fetch_array() salvo que devuelve el valor de la primera columna del resultado.
Esta es la forma óptima de obtener datos cuando solamente se requieren los valores de una columna del 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.
- decodificar_binario
-
When the decode_binary parameter is set to TRUE (the default), PHP will decode the binary encoding it applied to the data if it was encoded using the sqlite_escape_string(). You should normally leave this value at its default, unless you are interoperating with databases created by other sqlite capable applications.
Ejemplos
Example #1 Ejemplo de sqlite_fetch_single()
<?php
if ($manejador_bd = sqlite_open('mysqlitedb', 0666, $error_sqlite)) {
$consulta = "SELECT id FROM usuarios WHERE id = 42";
$resultado = sqlite_query($manejador_bd, $consulta);
if (sqlite_num_rows($resultado) > 0) {
echo sqlite_fetch_single($resultado); // 42
}
sqlite_close($manejador_bd);
}
?>
sqlite_fetch_single
24-Jul-2005 02:45
11-Jul-2004 10:02
Be careful with the use of sqlite_fetch_single() in a while-loop:
<?php
$result = sqlite_query($db_handle, 'SELECT foo FROM bar;');
while ($row = sqlite_fetch_single($result)) {
/*...do s.th. with $row...*/
}
?>
This may not work as expected because the loop will stop if any of the foos has a false value (e.g. zero).
Use sqlite_has_more() instead:
<?php
$result = sqlite_query($db_handle, 'SELECT foo FROM bar;');
while (sqlite_has_more($result)) {
$row = sqlite_fetch_single($result);
/*...do s.th. with $row...*/
}
?>
