Reply to thread

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.

[CODE=php]try {


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


} catch (PDOException $e) {


    echo $e->getMessage();

}[/CODE]


Here we have selected a table and we want to know how many data we select in this table.

[CODE=php]$userscount = $db->prepare('SELECT COUNT(*) FROM users');[/CODE]


I leave it blank because I will not enter an ID value or want any information.

[CODE=php] $userscount->execute();[/CODE]


Finally we will use it now. fetchColumn(); We want to shoot them all using the codes as follows;

[CODE=php]$alluserscount = $userscount->fetchColumn();[/CODE]


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.

[CODE=php]echo $alluserscount[/CODE]


Top