对于成功的数据库搜索,查询正在正常执行,但对于不成功的搜索,它仍然遵循成功的执行路径。对于不成功的Mysql数据库查询,是否有不同的方法来分析$ result?
$query = "SELECT password
FROM consumer
WHERE username = ? ";
//prepare query
if ($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
if (!is_null($result) && empty($stmt->last_error) && count($result)>0) {
//successful query, going to this every time even when there
//is no row in the database that matches the query
} else {
//unsuccessful query
}
答案 0 :(得分:0)
使用count()
几乎可以帮助您,除了一个小问题。即使没有匹配的密码,仍会返回单个结果,因此count
不会返回零。相反,您可以尝试使用mysqli_num_rows
,它返回受查询影响的记录数。如果是空结果集,则应返回0,因此以下检查应该起作用:
$rowcount = mysqli_num_rows($result);
if ($rowcount == 0) {
// unsuccessful query
}