0

This is the warning Im getting:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/snissa4/public_html/test.php on line 10

<?php
if(isset($_POST['user']))
{
    //variable declaration 
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    //connect to data base 
    $con=mysqli_connect("engr-cpanel-mysql.engr.illinois.edu","socialdrinkers_b","testing123","socialdrinkers_db");

    if (mysqli_num_rows(mysqli_query($con, "Select * from Drinkers where userID = '$user' AND password = '$pass'")))
    { // correct info
        $result = mysqli_query("SELECT * FROM Drinkers where userID = '$user' AND password = '$pass'");
        while($row = mysqli_fetch_array($result));
        {//cookie implementation
            $expire = time() + 60*60*24; //1 day
            setcookie('idNum', $row['idNum'], $expire); 
            echo "Logged in as <b>".$row['userID']."</b>";
        }
    } 
    else 
    { // wrong info 
        echo "<b>wrong id or pass</b>"; 
    }
}


echo "<form method = 'post'> 
Username: <input type = 'text' name = 'user'>
Password: <input type = 'password' name = 'pass'>
<input type = 'submit' value = 'LOG IN'> 
</form>";

 ?>

I am not sure why I am getting this warning, and when i try to login with the username and password i made in my database, it just keeps redirecting me to this page instead of giving the success message. I cannot find out why.

Thank you for your help, struggling with my cs411 (databases) class lol

Krish R
  • 22,583
  • 7
  • 50
  • 59
user2981811
  • 33
  • 1
  • 5

3 Answers3

0

mysqli_query() is returning false. Try echo mysqli_error($con); -- that will tell you why mysqli_query() is failing.

As others have pointed out, your code is very vulnerable to SQL injections. You should use prepared statements to fix that.

And as @viakondratiuk pointed out, you should rewrite your code. Right now, you execute your SELECT query twice. This just bogs down your script and will make it much harder to maintain.

elixenide
  • 44,308
  • 16
  • 74
  • 100
0

You should do it this way:

$result = mysqli_query($con, "Select * from Drinkers where userID = '$user' AND password = '$pass'");
$row_cnt = mysqli_num_rows($result);

And of course don't forget about sql injections.

You can read this topic to avoid them.

Community
  • 1
  • 1
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81
-1

Can you try this,

  $result = mysqli_query("SELECT * FROM Drinkers where userID = '".mysqli_real_escape_string($user)."' AND password = '".mysqli_real_escape_string($pass)."'") or die("Error " . mysqli_error($con));

    if (mysqli_num_rows($result) >0)
    { // correct info
        while($row = mysqli_fetch_array($result));
        {//cookie implementation
            $expire = time() + 60*60*24; //1 day
            setcookie('idNum', $row['idNum'], $expire);
            echo "Logged in as <b>".$row['userID']."</b>";
        }
    }else{ // wrong info
        echo "<b>wrong id or pass</b>";
    }
Krish R
  • 22,583
  • 7
  • 50
  • 59