Viewed   125 times

currently I am using:

$result = new SQLite3(sprintf("users/USERIDS_DB.sqlite"));

$numRows = $result->exec ("SELECT count(*) FROM USERIDS");

echo sprintf("the number of rows are: %d", $numRows);

but the result is 1 when it should be 6 (the number of rows I created using firefox sqlite3 addon)

Can anybody help please?



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.

Tuesday, August 9, 2022

You are probably getting a mySQL error when the statement is being prepared, but you don't have PDO configured to throw an exception in case of an error.


  $dbh = new PDO( /* your connection string */ );
  // . . .
Saturday, October 1, 2022

I solved the problem. Unfortunatetly the error message is confusing, because PHP did not find the file. Use always absolute paths, to avoid this error even if permissions, user and group are set correct.

Friday, November 4, 2022


$sql=$db->prepare("SELECT * FROM table WHERE selector=:selector");
$arr = array('count' => $count)
echo json_encode($arr);

Add following script to you index file

$(document).ready(function() {

function ajaxcall() { 
 type: "GET",
 url: "count.php"
 success: function(response){
     json_object = JSON.parse(response)
     var count = json_object.count
     /// set this count variable in element where you want to
     /// display count


If you don't want to use Jquery than you can try :

function ajaxcall() { 
   var xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
   if (xhttp.readyState == 4 && xhttp.status == 200) {
       json_object = JSON.parse(xhttp.responseText)
       var count = json_object.count
       /// set this count variable in element where you want to
       /// display count
   };"GET", "count.php", true);
   setTimeout(ajaxcall, 1000)

And add onload event on your body tag

 <body onload="setTimeout(ajaxcall, 3000)" >

or you can call ajax on some button click

<button onclick="setTimeout(ajaxcall, 3000)">clickme</button>
Saturday, November 19, 2022

You can efficiently get a count of all entities of a particular kind (i.e., number of rows in a table) using the Datastore Statistics. Simple example:

from google.appengine.ext.db import stats
kind_stats = stats.KindStat().all().filter("kind_name =", "NameOfYourModel").get()
count = kind_stats.count

You can find a more detailed example of how to get the latest stats here (GAE may keep multiple copies of the stats - one for 5min ago, one for 30min ago, etc.).

Note that these statistics aren't constantly updated so they lag a little behind the actual counts. If you really need the actual count, then you could track counts in your own custom stats table and update it every time you create/delete an entity (though this will be quite a bit more expensive to do).

Update 03-08-2015: Using the Datastore Statistics can lead to stale results. If that's not an option, another two methods are keeping a counter or sharding counters. (You can read more about those here). Only look at these 2 if you need real-time results.

Saturday, September 17, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :