简体   繁体   中英

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.....

You are looping through all the rows returned from the "mysqli_fetch..." command. Your "if" and "else" is useless -- you will always have rows. If you get no rows, you do not even enter the body of the while loop.

You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.

As others have pointed out, your else statement will never run. If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.

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 :

$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)

Currently, you are just checking either there is something inside $row , and this condition will never be wrong unless you will assign it 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.

you have to simply one thing, that is to change if condition like given below...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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