[Editor's note: to get short column names there's an undocumented PRAGMA setting. You can exec "PRAGMA short_column_names = ON" to force that behavior.]
I noticed that if you use Joins in SQL queries, the field name is messed up with the dot!
for example if you have this query:
SELECT n.*, m.nickname FROM news AS n, members AS m WHERE n.memberID = m.id;
now if you want to print_r the results returned using SQLITE_ASSOC type, the result array is like this :
array
(
[n.memberID] => 2
[n.title] => test title
[m.nickname] => NeverMind
[tablename.fieldname] => value
)
and I think it looks horriable to use the variable ,for example, $news['m.nickname'] I just don't like it!
so I've made a small function that will remove the table name (or its Alias) and will return the array after its index is cleaned
<?php
function CleanName($array)
{
foreach ($array as $key => $value) {
//if you want to keep the old element with its key remove the following line
unset($array[$key]);
//now we clean the key from the dot and tablename (alise) and set the new element
$key = substr($key, strpos($key, '.')+1);
$array[$key] = $value;
}
return $array;
}
?>
sqlite_fetch_array
SQLiteResult::fetch
SQLiteUnbuffered::fetch
(PHP 5 < 5.4.0, PECL sqlite >= 1.0.0)
sqlite_fetch_array -- SQLiteResult::fetch -- SQLiteUnbuffered::fetch — Scarica in un array la riga successiva da un set di risultati
Descrizione
$result
[, int $result_type = SQLITE_BOTH
[, bool $decode_binary = true
]] )Stile orientato agli oggetti (metodo):
$result_type = SQLITE_BOTH
[, bool $decode_binary = true
]] )$result_type = SQLITE_BOTH
[, bool $decode_binary = true
]] )
Scarica la riga successiva dal set di risultati indicato da result.
Se non vi sono righe successive, restituisce FALSE, altrimenti restituisce
un array associativo rappresentante i dati della riga.
Elenco dei parametri
-
result -
Risorsa risultato di SQLite. Questo parametro non è richiesto nella versione ad oggetti.
-
result_type -
Il parametro opzionale
result_typeaccetta una costante e determina come l'array restituito sarà indicizzato. UsandoSQLITE_ASSOCrestituirà solo indici associativi (campi nominali) mentreSQLITE_NUMrestituirà solo indici numerici (campi numerici ordinali).SQLITE_BOTHrestituirà sia gli indici associativi che numerici.SQLITE_BOTHè il default per questa funzione. -
decode_binary -
Quando
decode_binaryè impostato aTRUE(per default), il PHP decodificherà i dati binari se questi furono codificati con la funzione sqlite_escape_string(). Normalmente si dovrebbe lasciare inalterato il comportamento di default, a meno che non si debba intervenire su database creati da altre applicazioni SQLite.
Valori restituiti
Restituisce un array della riga successiva dal set di risultati, FALSE se
la posizione successiva è oltre l'ultima riga.
I nomi delle colonne restituiti da
SQLITE_ASSOC e da SQLITE_BOTH saranno
maiuscoli o minuscoli in base al valore del parametro di configurazione
sqlite.assoc_case
.
Esempi
Example #1 Esempio procedurale
<?php
$dbhandle = sqlite_open('sqlitedb');
$query = sqlite_query($dbhandle, 'SELECT name, email FROM users LIMIT 25');
while ($entry = sqlite_fetch_array($query, SQLITE_ASSOC)) {
echo 'Nome: ' . $entry['name'] . ' E-mail: ' . $entry['email'];
}
?>
Example #2 Esempio ad oggetti
<?php
$dbhandle = new SQLiteDatabase('sqlitedb');
$query = $dbhandle->query('SELECT name, email FROM users LIMIT 25'); // set di risultati bufferizzati
$query = $dbhandle->unbufferedQuery('SELECT name, email FROM users LIMIT 25'); // set di risultati senza buffer
while ($entry = $query->fetch(SQLITE_ASSOC)) {
echo 'Nome: ' . $entry['name'] . ' E-mail: ' . $entry['email'];
}
?>
Vedere anche:
- sqlite_array_query() - Esegue una query in un dato database e restituisce una matrice
- sqlite_fetch_string() - Alias di sqlite_fetch_single
