2015-06-22 202 views
-2

我知道如何使用mysqli搜索来查找是否至少有一个匹配。我如何去检索找到的行中的另一个值。它只需要改变1或2件事情。PHP(mysqli)找到单个值

想象我有以下DB:

ID  |  emailaddress  | password 
1  |  [email protected] | DUMB1PASS 
2  |  [email protected] | DUMB2Pass 

我可以使用下面的代码来验证如果的电子邮件地址“[email protected]”的存在。我将如何查找与该行相关的密码(即包含[email protected]的行)。

$email = "[email protected]"; 
$servername = "correct"; $username = "correct"; $DBpass = "correct"; $dbname = "correct"; 
    $conn = new mysqli($servername, $username, $DBpass, $dbname); 
    if ($conn->connect_error) 
     { 
      die("Connection failed: " . $conn->connect_error); 
     } 

    $sql = "SELECT emailaddress FROM registration WHERE emailaddress = '$email'"; 
    $result = $conn->query($sql); 

    if ($result->num_rows > 0) 
     { 
      echo "we found a match"; 
     } 
    else 
     { 
      echo "we did not find a match"; 
     } 

我想我可以这样做:

$email = "[email protected]"; 
$servername = "correct"; $username = "correct"; $DBpass = "correct"; $dbname = "correct"; 
    $conn = new mysqli($servername, $username, $DBpass, $dbname); 
    if ($conn->connect_error) 
     { 
      die("Connection failed: " . $conn->connect_error); 
     } 

    $sql = "SELECT password FROM registration WHERE emailaddress = '$email'"; 
    $result = $conn->query($sql); 

    if ($result->num_rows > 0) 
     { 
      echo "the value is '$result'"; 
     } 
    else 
     { 
      echo "we did not find a match"; 
     } 

但是,它会产生这样的错误:

Catchable fatal error: Object of class mysqli_result could not be converted to string in MYPAGE on line XX. 

我认为,这种情况的原因可能是$结果是数组或其他东西。但是,我不太了解sql/php知道这是否是问题,或者如果是这样的话如何从中得出结果。

我非常感谢任何帮助。

+1

如果你要去想找到对用户的密码匹配,那么你的系统是完全不安全的使用。告诉我,我不明白这个问题。如果这是为了匹配用户输入密码和散列密码,那么这是一个不同的故事。 –

+0

只是以此为例。但是我很困惑你的评论。如果密码是使用md5加密的,并且所有内容都是针对注入进行了消毒处理,那么您是不是该如何去检查登录系统的密码? – COMisHARD

+0

我不会用MD5,而是'password_hash()'或兼容包。重新加载我上面的评论,我做了一个修改;很确定这就是你可能真正想要说的。 –

回答

0

您需要先读取该行,试试这个:

if ($result->num_rows) { 
    $row = $result->fetch_row(); 
    echo 'the value is: ', $row[0]; 
}