简体   繁体   English

ELSE 条件在 PHP 的 while 循环中不起作用

[英]ELSE condition is not working in the while loop in PHP

<?php
    $conn=mysqli_connect("localhost","id6755695_artemi8","sharanod"
    ,"id6755695_user_info");
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    $department = $_POST["department"];
    $year = $_POST["year"];
    $sem = $_POST["semester"];
    $reg = $_POST["regulation"];

    $sql = "SELECT  book_name, author, edition, image FROM dept_search WHERE 
    department='$department' AND year='$year' AND semester='$sem' AND 
    regulations='$reg' ";
    $result=mysqli_query($conn,$sql);

    while($row = mysqli_fetch_assoc($result))
    {
        if($row)
        {
            echo "<br>";
        ?>
        <img src=" <?php echo $row['image']; ?> " height="300" width="300">

        <?php
        echo "<br>";

        echo "<b>",$row['book_name'],"</b>";
        echo "<br>";
        echo $row['author'];
        echo "<br>";
        echo $row['edition'];

        }
        else
        {
            echo "sorry book not found";
        }
    }

    mysqli_close($conn);


?>

please help me with this code,i am building a library management system.. The thing is I should be able to display the books if the given values are present i have in the database if not book not found must be displayed but in while loop after if, else does not runs.....请帮我处理这段代码,我正在构建一个图书馆管理系统.. 问题是如果给定的值存在我应该能够显示书籍如果没有找到书籍必须显示但在while循环中if 之后,else 不会运行.....

You are looping through all the rows returned from the "mysqli_fetch..." command.您正在遍历从“mysqli_fetch...”命令返回的所有行。 Your "if" and "else" is useless -- you will always have rows.你的“if”和“else”没用——你总是会有行。 If you get no rows, you do not even enter the body of the while loop.如果你没有得到任何行,你甚至不会进入 while 循环的主体。

You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.您需要对返回的行进行计数 (count($row)) 并显示一条消息,如果计数小于 1,则未找到任何内容。

As others have pointed out, your else statement will never run.正如其他人指出的那样,您的else语句永远不会运行。 If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.如果您已经在while循环中,您肯定会定义$row并且因此,else 永远不会运行。

What you can do is, check beforehand if the query returned actual results, like so:可以做的是,事先检查查询是否返回了实际结果,如下所示:

$result=mysqli_query($conn,$sql);

if($result->num_rows > 0){
   while($row = mysqli_fetch_assoc($result)){
      echo "<br>";
      ?>
      <img src=" <?php echo $row['image']; ?> " height="300" width="300">
      <?php
      echo "<br>";
      echo "<b>",$row['book_name'],"</b>";
      echo "<br>";
      echo $row['author'];
      echo "<br>";
      echo $row['edition'];
  }
}else{
    echo "Sorry book not found";
}

You can try with mysqli_num_rows .. sample code as follows :您可以尝试使用 mysqli_num_rows .. 示例代码如下:

$rowcount=mysqli_num_rows($conn,$sql);
if($rowcount!=0){
$result=mysqli_query($conn,$sql);

 while($row = mysqli_fetch_assoc($result))
 {
    echo "<br>";
?>

All you need to do is that you have to change if the condition from if($row) to if($other_condition)您需要做的就是将if($row)条件更改为if($other_condition)

Currently, you are just checking either there is something inside $row , and this condition will never be wrong unless you will assign it null .目前,您只是检查$row是否有内容,除非您将其分配为null否则此条件永远不会出错。 Because where $row will have something then while loop will be executed, and when while loop will be executed then if condition will be executed.因为$row会有东西然后 while 循环将被执行,当 while 循环将被执行然后 if 条件将被执行。

you have to simply one thing, that is to change if condition like given below...你必须做一件事,那就是改变如果条件如下所示......

if($row['value'] == 'something')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM