dongxingguo1978
dongxingguo1978
2018-01-12 20:15

简单的其他与循环不工作

已采纳

My code is working as for my needs. But the only thing bugging me is the "else" is not working. When i search for a correct record the record will appear and it was running fine. But if i Incorrectly search a record nothing will happen. i am expecting "Records not Found" will echo but nothing happen.


}else{
       echo "Records not found";
    }

This is the whole code.

      <?php
$conn = mysqli_connect("localhost", "root", "", "my1stdb") or die("could not connect");

$set = $_POST['search'];


if ($set) {
    $show   = "SELECT * FROM users where email='$set'";
    $result = mysqli_query($conn, $show);
    while ($rows = mysqli_fetch_array($result)) {
        echo "Registrant Found";
        echo "<tr>";
        echo "<td>";
        echo $rows['username'];
        echo "</td>";
        echo "<td>";
        echo $rows['fullname'];
        echo "</td>";
        echo "<td>";
        echo $rows['password'];
        echo "</td>";
        echo "<td>";
        echo $rows['email'];
        echo "</td>";
        echo "</tr>";
        echo "<br/>";
    }

} else {
    echo "Records not found";
}

?>
</table>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongzanghui4624 dongzanghui4624 3年前

    You need to use mysqli_num_rows() along with mysqli_fetch_assoc():-

    <?php
        $conn=mysqli_connect("localhost","root","","my1stdb") or die("could not connect");
    
        $set = $_POST['search']; 
    
    
        if($set) {
            $show="SELECT * FROM users where email='$set'";
            $result=mysqli_query($conn,$show) or die(mysqli_error($conn));
            if(mysqli_num_rows($result)>0){ // check data present or not
                while($rows=mysqli_fetch_assoc($result)){ // for lighter array due to associative indexes only
                    echo "Registrant Found";
                    echo "<tr>";
                    echo "<td>";
                    echo $rows['username'];
                    echo "</td>";
                    echo "<td>";
                    echo $rows['fullname'];
                    echo "</td>";
                    echo "<td>";
                    echo $rows['password'];
                    echo "</td>";
                    echo "<td>";
                    echo $rows['email'];
                    echo "</td>";
                    echo "</tr>";
                    echo "<br/>";
                }
            }else{
                echo "Records not found";
            }
        }else{
            echo "Please insert search term";
        }
    
    ?>
    </table>
    

    Note:- Your code is wide-open for SQL INJECTION. to prevent from it use prepared statements

    Reference:-

    mysqli prepared statements

    PDO prepared statements

    点赞 评论 复制链接分享
  • dqnqpqv3841 dqnqpqv3841 3年前

    You could count the number of results returned.

    if($set) {
        $show="SELECT * FROM users where email='$set'";
        $result=mysqli_query($conn,$show);
    
        $recordCount = 0;
    
        while($rows=mysqli_fetch_array($result)){
            $recordCount++;
                echo "Registrant Found";
                echo "<tr>";
                echo "<td>";
                echo $rows['username'];
                echo "</td>";
                echo "<td>";
                echo $rows['fullname'];
                echo "</td>";
                echo "<td>";
                echo $rows['password'];
                echo "</td>";
                echo "<td>";
                echo $rows['email'];
                echo "</td>";
                echo "</tr>";
                echo "<br/>";
        }
        if($recordCount==0){
            echo "Records not found";
        }
    
    }
    
    点赞 评论 复制链接分享

为你推荐