PHP Velho Oeste 2024

sqlite_fetch_array

(PHP 5 < 5.4.0, PECL sqlite >= 1.0.0)

sqlite_fetch_arrayObtém a próxima linha de um resultado como uma matriz.

Descrição

sqlite_fetch_array ( resource $result [, int $result_type [, bool $decode_binary ]] ) : array

Obtém a próxima linha do result indicado. Se não houverem mais linhas, retorna FALSE, em outro caso retorna uma matriz associativa representando os dados da linha.

result_type pode ser usado para especificar como você quer que os resultados sejam retornados. O valor padrão é SQLITE_BOTH o qual retorna as colunas com ambos os índices, numérico e pelo nome da coluna. SQLITE_ASSOC faz com que a matriz tenha apenas o índice dos nomes das colunas, e SQLITE_NUM tenha apenas o índice numérico.

Os nomes das colunas retornados por SQLITE_ASSOC e SQLITE_BOTH irão diferenciar maiúsculas e minúsculas de acordo com o valor da opção de configuração sqlite.assoc_case.

Quando decode_binary for TRUE (o padrão), o PHP irá decodificar a codificação binária que foi aplicada aos dados se eles foram codificados usando sqlite_escape_string(). Você irá normalmente deixar este valor como o seu padrão, a menos que você esteja interagindo com banco de dados de outras aplicações que usem o sqlite.

Veja também sqlite_array_query() e sqlite_fetch_string().

add a note add a note

User Contributed Notes 1 note

up
0
saleh at sfsj dot net
19 years ago
[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;
}
?>
To Top