PHP Knowledge Base
Row count with PDO
fetchColumn() Example #11
$sql__count = "SELECT count(*) from $whichTable";
$stmt__count = $conn->query($sql__count);
$row__count = $stmt__count->fetchColumn();
if ( $row__count > 0 ) {
// some 'green light' code
} else {
// some 'red light' code
}
fetchColumn() Example #2
$sql__tag_ID = "SELECT COUNT(*) FROM $whichTable where text__eng LIKE '%$db__tag_id%'"; $stmt__tag_ID = $conn->query($sql__tag_ID); $count__tag_ID = $stmt__tag_ID->fetchColumn();
rowCount() Example #1[2] [3]
$select = $db->prepare('SELECT username FROM users WHERE id = ?');
$select->execute([$id]);
if ($select->rowCount() > 0) {
//Row has been found
//Do ......
} else {
//No row found
//Do....
}
see also Example of a MySQLi Object-oriented SELECT Statement