dongxingguo1978 2018-01-12 20:15
浏览 10
已采纳

简单的其他与循环不工作

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 2018-01-12 20:20
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊