Count number of records in a database

Asked

Viewed 1,246 times

3

I want to count the number of records in a database table. I have 3 records in my DB, but the result is always 1. I used this code:

$base_hndl = new SQLite3("p.sqlite");
$select = "SELECT COUNT(*) FROM users";
$resultat = $base_hndl->exec($select);                      
echo "Utilizadores conectados ".$resultat;

2 answers

4


If you look at documentation will see that the return of the method exec is of the boolean type, ie it will return 0 (false) whether the operation failed or 1 (true) if the operation was successful. Therefore you always receive 1, the operation is always occurring smoothly.

The exec is to run no darlings, that is, to execute commands that do not give results except by the information if it occurred without problems. Typical cases are the CREATE, The INSERT, etc..

When you are searching for information in the database, that is, you are using SELECT need to use methods that bring these results to a variable or can be read with other class methods.

To get the result you want you will probably need the method QuerySingle() or possibly the Query. This may not be the case now, but the Prepare() may also be useful in other situations.

There are still other ways to access this, so it’s good to take a look at class documentation as a whole, you can learn a lot of cool stuff there, especially with the examples.

Other reference of the same manual with more complete information and other ways to recover the required data using a slightly different class than the one you are using. Take a good look, it may be more advantageous to exchange your code to use this class.

  • That’s right, thank you

3

Try it this way:

$select = "select count(*) as numeroOnline from users;";
$r = $base_hndl->exec($select);
$value = $r->fetch(PDO::FETCH_ASSOC);
Echo "número online ".$value['numeroOnline'];

I always gave 1 because you were getting true or false from the exec function. That’s if you’re sweating PDO.

  • 2

    An observation, in the first line it is seen that he is using the native class, and not PDO.

  • Well observed, I was in my Tablet and I didn’t even notice. I never made use of the native class Sqlite3, but I use in much the PDO Class in my design and recommend, even with use of sqlite, both for performance and security. I can’t tell you anything about the native class, but if there’s anyone who knows her,");

  • 3

    Without going into detail, native classes are either more efficient and secure than PDO, or at most equal. PDO is a compatibility layer, and internally uses the same native API as each of the Dbs in the same way as the native classes, but with more code on top to simulate the same behavior across all Dbs, which increases the problem exposure area. The advantage of PDO is that once you learn how to use it, you make it the same for all Dbs (and lose what’s special about each one, in exchange for easier code to reuse). The choice depends basically on the application proposal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.