1

So moving on from my last question yet again i have another problem. Here is the link for that however i will put the codes here too:Not getting login and password values in div So i'm trying to get the 'number' of the user im logging into as from the input fields which i have made in phpmyadmin. When i enter something completely random i get 0 as that randomly entered name isnt in my database, however when i enter the actual login and password from one of the users i get 0 instead of 1 which would mean that that user was found from the database. im not sure if i can link the database but here it is: http://localhost/phpmyadmin/sql.php?db=mysqldb1&table=users&token=dbc781132ba546cedab4644522745917&pos=0 here are the codes: here is login.html

<html>
    <head>
        <title> MYSQL </title>
        <script
         src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous">
        </script> 
    </head>
    <body>
    <div id="status"></div>
    <input id="login" placeholder="Login"><br>
<input type="password" id="pass" placeholder="Password"  <br>
    <button id="entry"> Login </button>
    <script>
    $("#entry").click(function(){
        $.post("check.php", {login: $("#login").val(), password: $("#pass").val()},
        function(result){
        $("#status").html(result);

    })
})
    </script>
    </body>
 </html>

here is the php code

<?php
$login=$_POST['login'];
$password=$_POST['password'];

echo $login.$password;

require "config/con1.php";

$sql= "SELECT id FROM users WHERE login='$login' AND password='$password' ";

$result=mysqli_query($con, $sql);
$value = mysqli_num_rows($result);
echo $value;

?>
  • 1
    I'm sure you're trying to learn but you should never be using plain text passwords. You might as well learn the proper way to begin with using password_hash and password_verify functions. – Devon May 17 '18 at 13:39
  • yeah, this was sort of a test run as i'm a beginner to all of this – unknown programmer May 17 '18 at 13:40
  • mysqli_num_rows will never return an id, as the name suggests, it's the number of rows returned from your query. You need to read the manual of these functions before using them so you understand what you're doing. php.net is well documented. – Devon May 17 '18 at 13:41
  • returning the id of the row though that's what i wanted so the user that's on lets say row 2 or 3 – unknown programmer May 17 '18 at 13:45
  • row 2 or 3 is subjective. A row with an id of 2 or 3 I assume is what you mean, in which, you'd need to fetch 'id' column from the result. – Devon May 17 '18 at 13:46
  • well i cant solve this at all i've tried everyhting that i know – unknown programmer May 17 '18 at 13:55
  • I don't think you've done enough research and training then. – Devon May 17 '18 at 13:57
  • so let's say i write the login and password of one of the users in my database, if it finds that user so let's say 'simon' it will show 1. which means found. i'm sorry for explaining it wrong – unknown programmer May 18 '18 at 11:48

1 Answers1

0

The php will return the number of rows. If you are looking for id you might need to fetch it.

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row['Column_in_database'];
    }
}
Parth Manaktala
  • 884
  • 8
  • 23