首页 > 解决方案 > PHP SQLite - Not returning correct count of match

问题描述

I am using PHP lite to search for a matching row like this...

$count = $db->exec("SELECT * FROM users WHERE userid = '34534fgr'");
echo $count;

But my count is returning 1 every time, even when the value does not exist.

Am I searching incorrectly?

标签: phpsqlite

解决方案


Am I searching incorrectly?

对。提示:调试时,使用var_dump代替echo,它会帮助你在这里看到问题,因为它会打印bool(true);而不是int(1)or string("1"),因为 PDO::exec() 返回一个布尔值。

这是您尝试做的事情的方法:

$count = $db->query("SELECT COUNT(*) FROM users WHERE userid = '34534fgr'",PDO::FETCH_NUM)->fetch()[0];

推荐阅读