Getting single value from SQLite database

I want to get the id of the most recently added item to my database and have the following statement

SELECT id FROM talks ORDER BY date_added DESC LIMIT 1;

The works using Firefox’s SQLite manager but I cannot work out the PHP I need to get the result. Can anyone help please?

EDIT

I have found that the following will do the job. However, is there an alternative to the foreach loop? Given there is only one result it seems rather daft!

$query  = 'SELECT id FROM talks ORDER BY date_added DESC LIMIT 1;';
$result = $db->query($query);
foreach ( $result as $row )
  $id = $row['id'];

Maybe
$result[0]['id']

though IMHO it would be wise to put it inside a conditional

Unfortunately that give a fatal error: Cannot use object of type PDOStatement as array

$id = $db->query($query)->fetchColumn();
1 Like

Brill. Thanks @Dormilich

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.