sqlite_fetch_single

(PHP 5)

sqlite_fetch_single

(no version information, might be only in CVS)

SQLiteResult->fetchSingle

(no version information, might be only in CVS)

SQLiteUnbuffered->fetchSingle -- Fetches the first column of a result set as a string

说明

string sqlite_fetch_single ( resource result [, bool decode_binary] )

Object oriented style (method):

class SQLiteResult {

string fetchSingle ( [bool decode_binary] )

}class SQLiteUnbuffered {

string fetchSingle ( [bool decode_binary] )

}

sqlite_fetch_single() is identical to sqlite_fetch_array() except that it returns the value of the first column of the rowset.

This is the most optimal way to retrieve data when you are only interested in the values from a single column of data.

参数

result

The SQLite result resource. This parameter is not required when using the object-oriented method.

decode_binary

decode_binary 参数设为 TRUE(默认值)时,PHP 将解码那些由 sqlite_escape_string() 编码的数据。通常应保留此值为其默认值,除非在操作其它支持 sqlite 程序建立的数据库时。

范例

例子 1. A sqlite_fetch_single() example

<?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);
}
?>


add a note add a note User Contributed Notes
jmarbas at hotmail dot com
24-Jul-2005 08: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
12-Jul-2004 04: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...*/
}
?>