Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   PHP LOGIN SCRIPT (https://www.warriorforum.com/programming/858288-php-login-script.html)

lloydblinks 18th October 2013 09:44 PM

PHP LOGIN SCRIPT
 
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);
?>

Delta90 19th October 2013 11:29 AM

Re: PHP LOGIN 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.

wordpressguru 20th October 2013 06:43 AM

Re: PHP LOGIN SCRIPT
 
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.

wordpressguru 20th October 2013 04:11 PM

Re: PHP LOGIN SCRIPT
 
Quote:

Originally Posted by vmb2013 (Post 8625227)
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?

lloydblinks 21st October 2013 10:52 AM

Re: PHP LOGIN SCRIPT
 
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);
?>

lloydblinks 21st October 2013 10:53 AM

Re: PHP LOGIN SCRIPT
 
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);
?>


All times are GMT -6. The time now is 10:22 PM.