【问题标题】:php :how i can get the unknown id of an username and password in mysqlphp:如何在mysql中获取用户名和密码的未知ID
【发布时间】:2018-12-20 21:44:33
【问题描述】:

大家好,我创建了一个登录名,它运行良好,所以同时我试图获取我输入的用户名和密码的 ID 谁能告诉我我该怎么做。注意:我在 php 中不是那么好。谢谢你

<?php
   include("login.php");
   session_start();
   $notexist="";


   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 


      $myusername = mysqli_real_escape_string($db,$_POST['email']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 
     $id;



      $sql = "SELECT ID FROM newdata WHERE  names = '$myusername' and password= '$mypassword' ";

      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);


      $count = mysqli_num_rows($result);


      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {

        $notexist="";
        echo "yes";

        echo $myusername;
       $url = "test.php?username=".$myusername;


       $_SESSION['favcolor'] = 'green';
       exit();
      }else {
         $notexist="wrong username and password";
         echo  $notexist;
      }
   }
?>

【问题讨论】:

  • lastnames 是您的密码栏吗?目前尚不清楚您使用此代码会得到什么行为。请使用该信息更新问题。
  • 注意:密码不应存储为纯文本。使用password_hash 对它们进行散列,并使用password_verify 进行验证。
  • echo $row['ID']; 也许???
  • AbraCadaver 是 $row['ID'] 它工作但我如何尝试获取名称或用户名 echo $row['names'];但它不起作用它只适用于 id 你能解释一下吗?
  • selectfrom 之间是您选择的列的 CSV。对于您当前的查询,您只需选择ID。但是返回用户名并没有什么意义,因为names = '$myusername' 意味着$myusernamenames 值。

标签: php mysql phpmyadmin


【解决方案1】:

编辑:对不起,我第一次在这里回答问题。我将在所有未来的答案中解释正在发生的事情。

基本上,当您使用 mysqli_fetch_array() 函数时,您将为查询的第一行返回两个数组。一个关联数组,key=字段名+值=返回值。一个从索引 0 开始的编号数组。您使用第二个可选参数指定只有关联数组。

因此,要访问此查询的第一个字段,您可以使用 $row['ID'] 或 $row[0]。我更喜欢使用关联数组,因为它使代码更易于阅读。

您不能使用它来访问字段名称或密码,因为它们不在查询的 SELECT 子句中。如果你想访问它们,你可以使用以下

$sql = "SELECT ID, names, password FROM newdata WHERE  names = '$myusername' and password= '$mypassword' ";

如果您返回多行数据,您可以通过将 fetch_array 包装在 while 循环内来访问每一行

while($row = mysqli_fetch_array($result)){
  //do some stuff here
}

【讨论】:

  • 是的,但如何?以及为什么此方法仅适用于 id 而不是名称或其他 ??
  • 感谢建设性的反馈,已经发布了更详细的解释
猜你喜欢
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
相关资源
最近更新 更多