简体   繁体   English

php:如何在mysql中获取用户名和密码的未知ID

[英]php :how i can get the unknown id of an username and password in mysql

hi everyone i create a login from and it working perfectly so at the same time i'm trying to get the ID of the username and password that i entered can anyone tell me how i can do that.大家好,我创建了一个登录名,它运行良好,因此同时我正在尝试获取我输入的用户名和密码的 ID,谁能告诉我如何做到这一点。 Note: i'm not that kinda good in php .注意:我不太擅长 php 。 thank you谢谢你

<?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;
      }
   }
?>

Edit: Sorry one of my first times answering a question on here.编辑:对不起,我第一次在这里回答问题。 I will include an explanation of what is happening in all future answers.我将在所有未来的答案中解释正在发生的事情。

basically when you use the mysqli_fetch_array() function you are returning two arrays for the first row of the query.基本上,当您使用 mysqli_fetch_array() 函数时,您将为查询的第一行返回两个数组。 One associative array, key=field name + value = return value.一个关联数组,键=字段名+值=返回值。 One numbered array starting from index 0. You specified to only have the associative array by using the 2nd optional parameter.一个从索引 0 开始的编号数组。您使用第二个可选参数指定只有关联数组。

So to access the first field of the query in this can you can use $row['ID'] or $row[0].因此,要访问此查询的第一个字段,您可以使用 $row['ID'] 或 $row[0]。 I prefer to use the associative array as it makes the code easier to read.我更喜欢使用关联数组,因为它使代码更易于阅读。

You can't use this to access the fields names or password as they are not in the SELECT clause of your query.您不能使用它来访问字段名称或密码,因为它们不在查询的 SELECT 子句中。 If you wanted to access them you could use the following如果你想访问它们,你可以使用以下

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

If you return multiple rows of data you can access each row by wrapping your fetch_array inside of a while loop如果返回多行数据,则可以通过将 fetch_array 包装在 while 循环中来访问每一行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM