System that counts the data in the table in the database ( I wrote it from Translator ) [PDO]

Novaytre

New Member
Jun 11, 2019
3
0
Hi friends. I wanted to open a help topic for friends who research PDO.
In this context, we find the number of data of the table in the database using PDO.


Let's connect the database first.
PHP:
try {

    $db = new PDO('mysql:host=localhost;dbname=DBNAME','root','');

} catch (PDOException $e) {

    echo $e->getMessage();
}

Here we have selected a table and we want to know how many data we select in this table.
PHP:
$userscount = $db->prepare('SELECT COUNT(*) FROM users');

I leave it blank because I will not enter an ID value or want any information.
PHP:
 $userscount->execute();

Finally we will use it now. fetchColumn(); We want to shoot them all using the codes as follows;
PHP:
$alluserscount = $userscount->fetchColumn();

Now we have entered our php query and we want to know the number of users immediately. So let's suppress our value with echo.
PHP:
echo $alluserscount
 

Users who are viewing this thread

Top