Sorry to bother you but I have spent hours browsing the internet for an answer to my question, I have searched Google and even asked on yahoo answers but I cannot find a solution to my problem.
I have a database, and in that database is a column called active and it is default to 0. In PHP I want to check to see if when a person is logging in if there account has a 0 in that column if so I log the user in but if not an error message appears saying their account has been banned. The problem is no matter what the message always appears:
Your account has been banned, if you think this is in error email: admin@website.com.
Here is the part of the code causing the problem:
$active = mysql_query("SELECT active FROM users WHERE username='$username'");
if (mysql_num_rows($active) == 0)
{
session_start();
$_SESSION['username'] = $username;
echo "You've been logged in. <a href='http://techhelpandhowtos.tk'>Home</a>";
}
else
{
echo "<font color=Red>Your account has been banned, if you think this is in error email: admin@website.com</font>";
}
A connection to the database is made earlier in the script so that is not the problem. Any help is appreciated. Please, can anyone find answer.
As long as your user exists (and his username is uniqe) mysql_num_rows will always return 1.
Use mysql_fetch_array like this:
$active = mysql_query("SELECT active FROM users WHERE username='$username'");
$res = mysql_fetch_array($active);
if($res["active"] === "1")
...
I should also mention, that you shouldn´t be using mysql extensions for new code, as it is currently deprecated and will be removed in the future. Use PDO or mysqli instead.
You were checking to see if there were matching records, NOT if they are active or not. The code below will select the user that matches the username, and check if they are banned or not.
$active = mysql_query("SELECT active FROM users WHERE username='$username'");
$row = mysql_fetch_assoc($active);
if (isset($row['active']) && $row['active'] == 1)
{
session_start();
$_SESSION['username'] = $username;
echo "You've been logged in. <a href='http://techhelpandhowtos.tk'>Home</a>";
}
else
{
echo "<font color=Red>Your account has been banned, if you think this is in error email: admin@website.com</font>";
}
i Think this should solve your problem
if (mysql_num_rows($active) == 1)
{
$result = mysql_result($active,0,'active');
if($result == 0)
{
session_start();
$_SESSION['username'] = $username;
echo "You've been logged in. <a href='http://techhelpandhowtos.tk'>Home</a>";
}else
{
echo "<font color=Red>Your account has been banned, if you think this is in error email: admin@website.com</font>";
}
}