无法通过查询对数据库验证用户

My code is not running.it always print $errmassage="username or password incorrect";. may be my query is not perfect.but i tried many time to count num_rows.but not working.please anybody help me.

$search= "SELECT * from registermember WHERE email='$username' AND password='$password'";
    $result=mysqli_query($conn,$search);
    $checkrow=mysqli_num_rows($result);
    if ($checkrow > 0) {
        $cookie=setcookie("$username",md5('$password'), date()+7200,"/");
        $_SESSION['user']=$username;
        header("location:mainpage.php");
    }
    else{
        $errmassage="username or password incorrect";
    }

First do

echo $search;
die;

And test the query on phpMyAdmin or another. Be sure the problem is code or SQL.