简体   繁体   中英

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. Note: i'm not that kinda good in 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. 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.

So to access the first field of the query in this can you can use $row['ID'] or $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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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