-2
$stmt = $conn->prepare('select count(names) as names from names where names = :name');
$stmt->bindParam(':name', $name);
$stmt->execute();

How do I output the value of names when doing a select count() using PDO without having to use a while loop or something similar?

I need the count value of names (1, 3 or 5 or whatever it is).

Norman
  • 6,159
  • 23
  • 88
  • 141

2 Answers2

1

The select count(..) from ..-Statement always only outputs this column (the count of rows), so you cannot access the names. You will have to execute a statement only for getting the names, OR you can actually output the name by yourself, because you already have it in $name ;)

Florian Müller
  • 7,448
  • 25
  • 78
  • 120
1
$count = $stmt->fetchColumn();

fetchColumn()

Nathan Q
  • 1,892
  • 17
  • 40
  • 1
    `execute` returns boolean. This will trigger `Call to a member on a non-object` error. – Leri May 03 '13 at 06:55