How to see the amount of results with PDO

0

Good evening,

How do I use, do I count the amount of results of a select in the database using PDO?

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,x-prototype-version,x-  requested-with');

include_once("conPDO.php");
$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

$email = $data->email;
$senha = $data->senha;

$login=$pdo->prepare("SELECT * FROM usuarios WHERE email=:email AND senha=:senha");
$login->bindValue("email", $email);
$login->bindValue("senha", $senha);
$login->execute();

$count = $login->fetch(PDO::FETCH_NUM);
print_r($count);
    
asked by anonymous 17.06.2016 / 05:15

1 answer

2

One of the ways I would use to do this count without affecting the records would be with FETCH_ASSOC.

Try to do so, instead of FETCH_NUM use $login->fetch(PDO::FETCH_ASSOC); and assign to $result . Then make count($result) and you will have the number of elements in the return array. That equals the number of lines. This way you can get the total number without touching the number of records or the model you are returning.

    
17.06.2016 / 05:43