MySQL / PHP:无法从数据库中选择但元素存在

时间:2013-07-20 21:19:52

标签: php mysql sql select mysqli

我知道这已被问了很多,但我似乎找不到合适的答案。在PHPMyAdmin中,用户名“user”和密码“test”存在 - 但由于某种原因,它一直说它没有。代码的输出是:

成功连接到DB电子邮件:* user *密码:* test *用户名和密码不匹配

以下是代码:

$con = new mysqli('somewhere.com','admin','*****','nameofdb');
    if($con->connect_errno > 0){
        die('Unable to connect to database [' . $con->connect_error . ']');
    }
    else{
        echo "Successfully connected to DB ";
    }
$email = mysqli_real_escape_string($con, $_POST['eml']);
       $password = mysqli_real_escape_string($con, $_POST['pwd']);
       //$encrPassword = md5($password);


       $login = $con->query("SELECT * 
                            FROM logininfo 
                            WHERE email = $email
                            AND password = $password");
       if($login == FALSE){
            echo " email:*$email* ";
            echo " password:*$password* ";
            //echo " password:*$encrPassword* ";

            echo "Username and Password does not Match";
       }
       else {
            echo "Hey this works";
       //Check if user is logged in, if not then redirect.     
            /*session_start();
            if($_SESSION['loggedin'] == FALSE){
                $_SESSION['loggedin'] = TRUE;
                $_SESSION['username'] = $username;
            }*/
            header("Location: ../browse/index.php");
       }
        mysqli_close($con);

3 个答案:

答案 0 :(得分:0)

查询出错,如果你回显查询并在mysql上测试它可能更容易调试,你可以这样做:

$query = "SELECT * 
          FROM logininfo 
          WHERE email = $email
          AND password = $password";
echo $query;

祝你好运!

答案 1 :(得分:0)

查询变量是否必须在它们周围加上引号?

SELECT * FROM sometable WHERE somecol ='$ someVar'AND someothercol ='$ someothervar';

答案 2 :(得分:0)

两个简单的步骤 1 ..在phpmyadmin中检查你的数据库,这些用户名和密码与你输入的用户名或密码相匹配。如果是echo查询,就知道查询内部是什么。 2 ..如果你正在使用,那么确保你是否在注册时使用md5加密,然后在登录时解密它,如

$password=md5($password);