|
sqlite_fetch_single
Fetches the first column of a result set as a string
(PHP 5, PECL sqlite:1.0.1-1.0.3)
Example 2335. A sqlite_fetch_single() example<?php Code Examples / Notes » sqlite_fetch_singlejmarbas
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
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...*/ } ?> |
Change Languagesqlite_array_query sqlite_busy_timeout sqlite_changes sqlite_close sqlite_column sqlite_create_aggregate sqlite_create_function sqlite_current sqlite_error_string sqlite_escape_string sqlite_exec sqlite_factory sqlite_fetch_all sqlite_fetch_array sqlite_fetch_column_types sqlite_fetch_object sqlite_fetch_single sqlite_fetch_string sqlite_field_name sqlite_has_more sqlite_has_prev sqlite_key sqlite_last_error sqlite_last_insert_rowid sqlite_libencoding sqlite_libversion sqlite_next sqlite_num_fields sqlite_num_rows sqlite_open sqlite_popen sqlite_prev sqlite_query sqlite_rewind sqlite_seek sqlite_single_query sqlite_udf_decode_binary sqlite_udf_encode_binary sqlite_unbuffered_query sqlite_valid |