[ Team LiB ] Previous Section Next Section

Selecting Data

We can use the sqlite_query() function to send a SELECT statement to SQLite. When we request data through sqlite_query(), we get a result resource in return. We can use this with other SQLite functions to access our data. After we have a result resource, we can access a row of data with sqlite_fetch_array():

$select = "SELECT * FROM people";
$res = sqlite_query( $dbres, $select );

while ( sqlite_has_more( $res ) ) {
  $row = sqlite_fetch_array( $res );
  print "row: {$row['id']} {$row['firstname']} {$row['secondname']}";
  print "<br />\n";

We call sqlite_query(), passing it our database resource, and a SELECT statement. We get a result resource in return. In production code, we would test the return value to ensure that it is a valid resource. The sqlite_has_more() function returns true if there is still more data to read in a resultset and false otherwise. We can therefore use it in the test expression of a while loop.

sqlite_fetch_array() returns an associative array of the current row in a resultset, and we print the elements of each row to the browser:

row: 1 John peel<br />
row: 2 mary biscuit<br />

Now that we have finished with our database for this request, we can call sqlite_close(). sqlite_close() requires a database resource and closes the connection to the database, freeing it up for other processes:

sqlite_close( $db );

    [ Team LiB ] Previous Section Next Section