PHP Doku:: Holt sich die erste Spalte eines Abfrageergebnisses als String - function.sqlite-fetch-single.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenSQLiteSQLite Funktionensqlite_fetch_single -- SQLiteResult->fetchSingle -- SQLiteUnbuffered->fetchSingle

Ein Service von Reinhard Neidl - Webprogrammierung.

SQLite Funktionen

<<sqlite_fetch_object -- SQLiteResult->fetchObject -- SQLiteUnbuffered->fetchObject

sqlite_fetch_string>>

sqlite_fetch_single

SQLiteResult->fetchSingle

SQLiteUnbuffered->fetchSingle

(PHP 5, PECL sqlite >= 1.0.1)

sqlite_fetch_single -- SQLiteResult->fetchSingle -- SQLiteUnbuffered->fetchSingleHolt sich die erste Spalte eines Abfrageergebnisses als String

Beschreibung

string sqlite_fetch_single ( resource $result [, bool $decode_binary = true ] )

Objektorientierter Stil (Methode):

string SQLiteResult::fetchSingle ([ bool $decode_binary = true ] )
string SQLiteUnbuffered::fetchSingle ([ bool $decode_binary = true ] )

sqlite_fetch_single() ist identisch mit sqlite_fetch_array(), außer dass es die erste Spalte der Abfragereihe liefert.

Es ist der beste Weg, um Daten zu bekommen, wenn man nur die ersten Spalte der Daten benötigt.

Parameter-Liste

result

Die Ressource des SQLite-Ergebnisses. Der Parameter wird bei der objektorientierten Notation nicht benötigt.

decode_binary

Falls der Parameter decode_binary auf TRUE gesetzt ist (das ist die Standardeinstellung), dekodiert PHP die Binärkodierung, die bei Verwendung von sqlite_escape_string() auf die Daten angewendet wird. Solange Sie die Daten nicht mit anderen SQLite-Fähigen Applikationen teilen, sollten Sie diesen Wert in der Standardeinstellung lassen.

Rückgabewerte

Liefert den Inhalt der ersten Spalte als eine Zeichenkette (string).

Beispiele

Beispiel #1 Ein sqlite_fetch_single()-Beispiel

<?php
if ($dbhandle sqlite_open('mysqlitedb'0666$sqliteerror)) {

    
$sql "SELECT id FROM sometable WHERE id = 42";
    
$res sqlite_query($dbhandle$sql);

    if (
sqlite_num_rows($res) > 0) {
        echo 
sqlite_fetch_single($res); // 42
    
}

    
sqlite_close($dbhandle);
}
?>

Siehe auch

  • sqlite_fetch_array() - Liest die nächste Zeile aus dem Datenbankergebnis und gibt sie als Array zurück


3 BenutzerBeiträge:
- Beiträge aktualisieren...
visavi dot net at mail dot ru (Vantuz)
8.08.2009 13:26
With the help of sqlite_fetch_single, you can quickly and easily calculate the number of elements in the table

<?php

// create new database (OO interface)
$db = new SQLiteDatabase('database.db', 0666);

// create table inbox and paste data
$db->queryExec("
CREATE TABLE inbox (
inbox_id INTEGER NOT NULL PRIMARY KEY,
inbox_send varchar(20) NOT NULL,
inbox_text TEXT NOT NULL,
inbox_time INTEGER UNSIGNED NOT NULL,
inbox_user varchar(20) NOT NULL
);"
);

$ins_query = "
INSERT INTO inbox (inbox_send, inbox_text, inbox_time, inbox_user) VALUES ('Ilia1', 'Message1', '"
.time()."', 'my_name');
INSERT INTO inbox (inbox_send, inbox_text, inbox_time, inbox_user) VALUES ('Ilia2', 'Message2', '"
.time()."', 'user');
INSERT INTO inbox (inbox_send, inbox_text, inbox_time, inbox_user) VALUES ('Ilia2', 'Message3', '"
.time()."', 'my_name');
"
;

$db->queryExec($ins_query);

//view all posts by my_name
$total = $db->singleQuery("SELECT count(*) FROM inbox WHERE inbox_user='my_name';");

echo
$total;
// the result will be the number 2

/* P.S. sorry for my english */
?>
jmarbas at hotmail dot com
24.07.2005 2:45
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);
roga at yoopee dot de
11.07.2004 22: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...*/
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)