PHP LOGIN SCRIPT

by 6 replies
8
i am trying to create a php login script but i am having this error

Warning: mysql_num_rows() expects parameter 1 to be resource, object given in C:\wamp\www\examSystem\login.php on line 57


THIS IS MY CODE

<?php

$sql = "SELECT * FROM studentlogin WHERE user_name = '$loginName' AND password = '$password'";
$result = mysqli_query($con,$sql) or die(mysqli_error("Error:"));
$count = mysql_num_rows( $result);
//if(mysql_num_rows(resource $result) == 1)

print("$count");

if( $count == 1)
{
header('Location: http:lecturer.html');
}
else
{
echo "Wrong Username or Password";
}
mysqli_close($con);
?>
#programming #login #php #script
  • I'm suspecting there's something wrong with your sql query could you test running it in phpmyadmin(if you have access to it) and see if the query is actually obtaining any info from the database.

    Code:
    And if you wrap your code in [ CODE] tags it's much easier to read.
    • [ 1 ] Thanks
  • Here it's your answer: http://www.facestatistic.com/test.php I have created this specially for you. All you will have to do is to modify the script where needed and add the AND condition on the query.
    Tell me if it is working.

    Regards.
    • [ 1 ] Thanks
  • Banned
    [DELETED]
    • [1] reply
    • What kind of error? Could you post your script and error here? Or, could you post a link to see this error?
      • [1] reply
  • Banned
    [DELETED]
    • [ 1 ] Thanks
  • thanks to every one the prolem was, i was making use of == instead of === and i was also mixing mysli_num_rows and mysl_num_rows.

    this is my new code and it is working perfectly.

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>

    //code



    <?php

    $users = $_POST["users"];

    $loginName = $_POST["loginName"];
    $password = $_POST["password"];


    $loginName = stripslashes($loginName);
    $password = stripslashes($password);

    $loginName = mysql_real_escape_string($loginName);
    $password = mysql_real_escape_string($password);


    $con = mysqli_connect("localhost","root","","online_exam" );
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();

    }

    if($users == "Administrator")
    {


    $sql = "SELECT * FROM adminlogin WHERE user_name = '".$loginName."' AND password = '".$password."'";


    $result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $row = mysqli_fetch_array($result);

    if($row['user_name'] === $loginName && $row['password'] === $password )
    {
    //echo $row['user_name'];


    header('Location:Admin.html');
    }
    else
    {
    echo "wrong password";
    }
    }

    else if($users == "Lecturer")
    {

    //$sql = "SELECT * FROM studentlogin WHERE user_name = '$loginName' AND password = '$password'";

    $sql = "SELECT * FROM adminlogin WHERE user_name = '".$loginName."' AND password = '".$password."'";
    $result = mysqli_query($con,$sql) or die(mysqli_error($con));



    $row = mysqli_fetch_array($result);

    if($row['user_name'] === $loginName && $row['password'] === $password )
    {
    //echo $row['user_name'];
    header('Location:lecturer.html');

    }
    else
    {
    echo "wrong password";
    }

    }
    else if($users == "Student")
    {
    $sql = "SELECT * FROM studentlogin WHERE user_name = '".$loginName."' AND password = '".$password."'";
    $result = mysqli_query($con,$sql) or die(mysqli_error($con));


    $row = mysqli_fetch_array($result);

    if($row['user_name'] === $loginName && $row['password'] === $password )
    {
    header('Location:student.html');
    }
    else
    {
    echo "wrong password";
    }
    }
    mysqli_close($con);
    ?>

Next Topics on Trending Feed

  • 8

    i am trying to create a php login script but i am having this error Warning: mysql_num_rows() expects parameter 1 to be resource, object given in C:\wamp\www\examSystem\login.php on line 57