There will be multiple entries with the same criteria in the table. I want to check if there is at least one entry that matches the criteria. Also, I have read on multiple posts around the web that EXISTS is faster than count().
i see. you could run your SQL statement in an administration tool like PHPMyAdmin first and have a look at what data structure you get back. Then you may try to give your result an alias. And finally you can fetch this result like any other statement and check for the contents.
$sql = "SELECT TRUE FROM users WHERE user_name = ? LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute(['John']);
$exists = $stmt->fetch();
if (!$exists) {
// no match found
}
True, but maybe the sample code is just a “for example”, and the requirement is for an entirely different usage that cannot be shared here for some reason.
It’s not username in the sense of login username. It is user’s name. Imagine a table of people some of which may share the exact same name.
Thank you all for your alternative suggestions. I am just trying to process the result that I get with the EXISTS syntax. I mean I want to learn how the returned data by an EXISTS statement is used after it is returned.