Using SQlite3 in PHP how to count the number of rows in a result set? Using SQlite3 in PHP how to count the number of rows in a result set? sqlite sqlite

Using SQlite3 in PHP how to count the number of rows in a result set?


$db = new SQLite3('filename.db3');$count = $db->querySingle("SELECT COUNT(*) as count FROM tablename");echo $count;


From the documentation:

public bool SQLite3::exec ( string $query )

Executes a result-less query against a given database.

This methods returns a boolean, not a result set. When you convert true to an integer it will become 1.

You should use SQLite3::query(). Example (untested):

$rows = $result->query("SELECT COUNT(*) as count FROM USERIDS");$row = $rows->fetchArray();$numRows = $row['count'];

Btw, naming the instance of the SQLite3 class $result can be misleading (especially in a DB environment). I would call it $db or $connection.


$result = $db->query("SELECT * FROM db_name")$row=$result->fetchArray(SQLITE3_ASSOC);    // check for empty result    if ($row != false) {      // do something here if record exists    }