6 replies
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);
?>
#login #php #script
  • Profile picture of the author Delta90
    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.
    {{ DiscussionBoard.errors[8622398].message }}
  • Profile picture of the author wordpressguru
    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.
    {{ DiscussionBoard.errors[8624159].message }}
  • Profile picture of the author vmb2013
    Banned
    [DELETED]
    {{ DiscussionBoard.errors[8625227].message }}
    • Profile picture of the author wordpressguru
      Originally Posted by vmb2013 View Post

      I'm also getting a similar error when i try to add adsense to my php site.
      What kind of error? Could you post your script and error here? Or, could you post a link to see this error?
      {{ DiscussionBoard.errors[8625274].message }}
      • Profile picture of the author lloydblinks
        this is my new code and it is working perfectly now.

        <?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);
        ?>
        {{ DiscussionBoard.errors[8627805].message }}
  • Profile picture of the author lloydblinks
    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);
    ?>
    {{ DiscussionBoard.errors[8627803].message }}

Trending Topics