所以我在MySQL中还很陌生。 我有一个表,里面没有数据(=空表)。 我正在从table.xx和table.YY的表中执行SELECT COUNT(*)。 结果为1,但应为0? (BC空表)。
注意:表的主要代码是table.id,因此没有一个被选择。
谢谢您的输入! 最好,
$req_testLogIn = $this->_db->prepare("SELECT COUNT(*) FROM users WHERE USERS_email='test' and USERS_pwd='test'");
$res_testLogIn = $req_testLogIn->execute();
if($res_testLogIn == 1){} //is true...
答案 0 :(得分:4)
答案 1 :(得分:0)
@LNFullStack,您在正确的位置上只是在检查查询是否成功,但是确实如此,因此您得到了1。因为您的查询成功,所以您的代码将始终为真。
您应该检查查询是否实际返回了一行,在这种情况下,您应该检查诸如行数之类的内容。
示例1:
$req_testLogIn = $this->_db->prepare("SELECT COUNT(*) FROM users WHERE USERS_email='test' and USERS_pwd='test'");
$res_testLogIn = $req_testLogIn->execute();
if($req_testLogIn->rowCount() ==1){...} //false because 0 rows found in table and false too if row is more than 1. Take note.
示例2:
$req_testLogIn = $this->_db->prepare("SELECT COUNT(*) as totalUsers FROM users WHERE USERS_email='test' and USERS_pwd='test'");
$res_testLogIn = $req_testLogIn->execute();
$db_rows = $res_testLogInt->fetch(PDO::FETCH_OBJ);
if($db_rows->totalUsers ==1){...} //false because 0 rows found in table and false too if row is more than 1. Take note.
我希望这会有所帮助。