PHP Velho Oeste 2024

sqlite_fetch_array

SQLiteResult::fetch

SQLiteUnbuffered::fetch

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

sqlite_fetch_array -- SQLiteResult::fetch -- SQLiteUnbuffered::fetch結果セットから次のレコードを配列として取得する

説明

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

オブジェクト指向型 (メソッド):

SQLiteResult::fetch ([ int $result_type = SQLITE_BOTH [, bool $decode_binary = TRUE ]] ) : array
SQLiteUnbuffered::fetch ([ int $result_type = SQLITE_BOTH [, bool $decode_binary = TRUE ]] ) : array

指定した結果ハンドル result から次のレコードを取得 します。レコードがもうない場合は FALSEを返し、それ以外は レコードデータを含む連想配列を返します。

パラメータ

result

SQLite 結果リソース。 このパラメータは、 オブジェクト指向言語型メソッドを使用する場合は不要です。

result_type

オプションの result_type パラメータには定数を指定でき、返される配列の添字を定義します。 SQLITE_ASSOC を用いると、連想配列の添字(名前フィールド)のみが 返されます。一方、SQLITE_NUM は、 数値の添字(フィールド番号)のみを返します。SQLITE_BOTH は、 連想配列の添字と数値の添字の両方を返します。 SQLITE_BOTH がこの関数のデフォルトです。

decode_binary

decode_binary パラメータが TRUE (デフォルト)に 設定された場合、PHP はバイナリエンコーディングをデコードします。 これは、sqlite_escape_string() によりエンコードされたデータに 適用されます。sqlite をサポートする他のアプリケーションにより 作成されたデータベースを処理する時以外は、この値をデフォルトのままにしておくべきです。

返り値

結果セットの次レコードの配列を返します。 次レコードの位置が最終レコード以降の場合、FALSE を返します。

SQLITE_ASSOC および SQLITE_BOTH で 返されるカラム名は、設定オプション sqlite.assoc_case の値に基づき、 大文字小文字が変換されます。

例1 手続き型言語スタイルでの例

<?php
$dbhandle 
sqlite_open('sqlitedb');
$query sqlite_query($dbhandle'SELECT name, email FROM users LIMIT 25');
while (
$entry sqlite_fetch_array($querySQLITE_ASSOC)) {
    echo 
'Name: ' $entry['name'] . '  E-mail: ' $entry['email'];
}
?>

例2 オブジェクト指向言語型スタイルでの例

<?php
$dbhandle 
= new SQLiteDatabase('sqlitedb');

$query $dbhandle->query('SELECT name, email FROM users LIMIT 25'); // buffered result set
$query $dbhandle->unbufferedQuery('SELECT name, email FROM users LIMIT 25'); // unbuffered result set

while ($entry $query->fetch(SQLITE_ASSOC)) {
    echo 
'Name: ' $entry['name'] . '  E-mail: ' $entry['email'];
}
?>

参考

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