Skip to content
PHP Logo

What is the best way to dynamically number results in a SQLite database query in PHP?

When querying a SQLite database in PHP, you can dynamically number the results using a counter variable in your PHP code. You can increment this counter variable for each row returned from the query to assign a unique number to each result.

php
<?php
// Connect to SQLite database
$db = new SQLite3('database.db');

// Execute query
$results = $db->query('SELECT * FROM table');

// Initialize counter
$counter = 1;

// Loop through results and dynamically number them
while ($row = $results->fetchArray()) {
    echo $counter . '. ' . $row['column_name'] . '<br>';
    $counter++;
}

// Close database connection
$db->close();
?>