mysqli语句没有显示其他没有结果 [英] mysqli statement not displaying else for no result
问题描述
我有一个声明,非常基本的,从数据库中提取并显示信息。有用。但是,如果我删除数据库中的条目,则else语句应该说没有结果。但事实并非如此。任何人都可以看到问题吗?
I have a statement, quite basic that pulls in from a database and displays the information. It works. However if I delete the entry in the database, the else statement should kick in a say that there are no results. However it does not. Can anyone see the problem?
这是代码,我正在采取的是if($ result){
This is the code, the bit i'm taking about is the if($result) {
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else {
$result = mysqli_query($con,"SELECT * FROM vacancies WHERE status ='0'");
if($result) {
while($row = mysqli_fetch_array($result)) {
echo "<li><a href=\"current_vacancy?id=".$row['id']. "\">". $row['title'] ."</a> <img src=\"rightarrow.png\" alt=\"next\"></li>";
}
} else {
// execute for 0 rows returned.
echo "There are currently no vacancies available";
}
}
谢谢
推荐答案
要实现无结果,您需要使用 mysqli_num_rows()
检查查询获取的行数。
To implementing no result, you need to check number of rows you fetch by query with mysqli_num_rows()
.
在您检查$ result变量的情况下,这将存储查询执行的状态,因此它将始终为true,因为查询将始终执行,即使它返回没有行。
In your case you checked for the $result variable this will store the status of the query execution, therefore it will always be true, since query will always execute even if it returns no rows.
这篇关于mysqli语句没有显示其他没有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!