如何检查php中是否存在数据库中的值


how to check whether a value is present in database or not in php

我知道这很简单。。但是我找不到错误。。我有一个登录页面,在那里我接受用户的输入,即用户名和密码。在另一个页面上,我检查数据库中是否存在值。但它既没有给我错误,也没有工作。。而且它没有进入第一个if条件。。if ($result->num_rows > 0)

<body style="background-color:lightgrey;">
    <?php  
    include('custdb.php');
    session_start();
    $uname=$_POST['username'];
    $pass=$_POST['password'];
    $sql = "SELECT * FROM `info` WHERE `username`='".$uname."';";
    //echo $sql;
    $result = $conn->query($sql);
    echo"1";
    echo $result;
    if ($result->num_rows > 0) 
    {
        echo"1";
        while($row = $result->fetch_assoc()) 
        {
            if($uname==$row["username"])
            {
                header("location:custprofile.php");
            }
            else
            {
                header("Location:custindex.php");
            }
        }   
    }
    else
    {
        echo "invalid input";
        echo '<h4 align="left"><a href="custindex.php">LOGIN</a> </h4>';    
    }
    ?>

尝试更改代码的这一部分,

$uname=$_POST['username'];
$pass=$_POST['password']; 
$sql = "SELECT * FROM `info` WHERE username='".$uname."' AND password='".$pass."'";