[英]mysqli statement not displaying else for no result
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.
但是,如果我删除数据库中的条目,else 语句应该会说没有结果。 However it does not.
然而事实并非如此。 Can anyone see the problem?
任何人都可以看到问题吗?
This is the code, the bit I'm taking about is the if($result) {
这是代码,我要讨论的是
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";
}
}
If the SELECT
operation was successful then mysqli_query()
will always return an object.如果
SELECT
操作成功,则mysqli_query()
将始终返回一个对象。 Whether this object holds 0 or more records it will always be true.无论这个对象拥有 0 条还是更多条记录,它都将始终为真。 What you should rather have done instead is fetch all the records from the result.
您应该做的是从结果中获取所有记录。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = new mysqli("localhost", "user", "pass", "db");
$con->set_charset('utf8mb4'); // always set the charset
$result = $con->query("SELECT * FROM vacancies WHERE status ='0'");
$data = $result->fetch_all(MYSQLI_BOTH);
if ($data) {
foreach ($data as $row) {
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";
}
To implementing no result, you need to check number of rows you fetch by query with mysqli_num_rows()
.要实现无结果,您需要使用
mysqli_num_rows()
检查通过查询获取的行数。
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.在您的情况下,您检查了 $result 变量,这将存储查询执行的状态,因此它始终为真,因为即使查询不返回任何行,它也将始终执行。
To make it more natural way让它更自然的方式
$data = [];
$result = $con->query("SELECT * FROM vacancies WHERE status ='0'");
while($row = mysqli_fetch_array($result)) {
$data[] = $row;
}
?>
and now you may use $data
the way you tried to use $result:现在您可以像使用 $result 一样使用
$data
:
<?php foreach ($data as $row): ?>
<li>
<a href="current_vacancy?id=<?=$row['id']?>">
<?=$row['title']?>
</a>
<img src="/images/rightarrow.png" alt="next">
</li>
<?php endforeach ?>
<?php if(!$data): ?>
There are currently no vacancies available
<? endif ?>
The result returned from mysqli_query
is an object, not a row count. mysqli_query
返回的结果是一个对象,而不是行数。 Use mysqli_num_rows($result)
to get the row count.使用
mysqli_num_rows($result)
获取行数。
By the way, be sure to call mysqli_free_result($result)
at the end.顺便说一句,一定要在最后调用
mysqli_free_result($result)
。 Suggested outline:建议大纲:
$result = mysqli_query(…);
if (!$result) {
# Handle failure
} else {
if (mysqli_num_rows($result) == 0) {
# Handle no vacancies
} else {
# Normal case
}
mysqli_free_result($result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.