逻辑PHP / SQL Server出错

时间:2022-04-19 15:44:39

I have a logic error I cannot for the life of me figure out. The problem is this very very basic login page I have written is always outputting the default answer as opposed the the choice from the table.

我有一个逻辑错误,我不能为我的生活弄清楚。问题是我写的这个非常非常基本的登录页面总是输出默认答案,而不是从表格中选择。

I am trying to return to the php the value in E_Type in this case the value should return A, however it is not.

我试图在E_Type中返回php的值,在这种情况下,值应返回A,但事实并非如此。

What am I doing wrong?

我究竟做错了什么?

Code:

<?php
$server   = -Removed;
$login    = -Removed;            
$pass     = -Removed;        
$login    = $_POST['login'];
$password = $_POST['password'];
$table    = 'USERPASS';
$table2   = 'EMPLOYEES';
$res      = 'q';

$dblink = @mssql_connect(-Removed) or die("Error 1");
mssql_select_db('group5', $dblink) or die( "unable to select the database");

$sqlquery = "SELECT E_TYPE FROM USERPASS U, EMPLOYEES E WHERE U.EMPLOYEE_ID = E.EMPLOYEE_ID AND PASSWORD = '$password' AND USERNAME = '$login'";
$res = mssql_query($sqlquery, $dblink) or die("Error5");
$count = mssql_num_rows($res);

if($count==1)
{
    if ($res == "A" )
    {
        echo "Success 1";
    }
    else if ($res == "B" )
    {
        echo "Success 2";
    }
    else
    {
        echo "Error...";
    }
}
?>

3 个解决方案

#1


1  

You need to fetch the result first. In $res you basically only have a result handle that can iterate over the results.

您需要先获取结果。在$ res中,您基本上只有一个可以迭代结果的结果句柄。

Use mssql_fetch_assoc to fetch the results and access them (this would loop over all resulting rows):

使用mssql_fetch_assoc获取结果并访问它们(这将遍历所有结果行):

while(($row = mssql_fetch_assoc($res)) !== FALSE) {
    echo $row['E_TYPE'];
}

In your case (if you only expect a single row) use this:

在你的情况下(如果你只想要一行)使用这个:

if($count == 1) {
    $row = mssql_fetch_assoc($res);
    if ($row['E_TYPE'] == "A" )
    {
        echo "Success 1";
    }
    else if ($row['E_TYPE'] == "B" )
    {
        echo "Success 2";
    }
    else {
        echo "Error...";
    }

}

#2


1  

mssql_query return MS SQL result set, to get returned value, you have to fetch row from this resultset first. See http://www.php.net/manual/en/function.mssql-fetch-assoc.php and other mssql_fetch functions

mssql_query返回MS SQL结果集,要获取返回值,必须先从此结果集中获取行。请参阅http://www.php.net/manual/en/function.mssql-fetch-assoc.php和其他mssql_fetch函数

#3


0  

<?php
$server = -Removed;
$login = -Removed;           
$pass = -Removed;        
$login=$_POST['login'];
$password=$_POST['password'];
$table= 'USERPASS';
$table2='EMPLOYEES';
$res='q';

$dblink = @mssql_connect(-Removed) or die("Error 1");
mssql_select_db('group5', $dblink) or die( "unable to select the database");
$sqlquery = "SELECT E_TYPE FROM USERPASS U, EMPLOYEES E WHERE U.EMPLOYEE_ID = E.EMPLOYEE_ID AND PASSWORD = '$password' AND USERNAME = '$login'";
$res = mssql_query($sqlquery, $dblink) or die("Error5");
$count=mssql_num_rows($res);
    if(1 == $count){
        $row=mssql_fetch_assoc($res);//fetch row from database
        switch($row['E_TYPE']) {
            case "A":
                echo "Success 1"; 
                break;
            case "B":
                echo "Success 2"; 
                break;
            default:
                echo "Error..."; 
                break;
        }
    }
}
mssql_free_result($res);
?>

you should learn about select...case statements. only thing I can see is the case of your characters. databases may or may not be case sensitive. you are also missing something which can sometimes affect your results: mysql_free_result(). you should usually do a while statement with regards to fetching rows rather than an if, because there may be more than 1 row.

你应该了解select ... case语句。我唯一能看到的是你角色的情况。数据库可能是也可能不区分大小写。你也遗漏了有时会影响结果的东西:mysql_free_result()。你应该通常做一个关于获取行而不是if的while语句,因为可能有超过1行。

I usually put the constant before the comparison operator and the variable on the right side when it's possible and it makes sense, because the compiler will catch the error if in any case it becomes an assignment because of a missing =.

我通常在比较运算符之前将常量和右侧的变量放在可能的情况下并且它是有意义的,因为如果在任何情况下由于缺少=而成为赋值,编译器将捕获错误。

#1


1  

You need to fetch the result first. In $res you basically only have a result handle that can iterate over the results.

您需要先获取结果。在$ res中,您基本上只有一个可以迭代结果的结果句柄。

Use mssql_fetch_assoc to fetch the results and access them (this would loop over all resulting rows):

使用mssql_fetch_assoc获取结果并访问它们(这将遍历所有结果行):

while(($row = mssql_fetch_assoc($res)) !== FALSE) {
    echo $row['E_TYPE'];
}

In your case (if you only expect a single row) use this:

在你的情况下(如果你只想要一行)使用这个:

if($count == 1) {
    $row = mssql_fetch_assoc($res);
    if ($row['E_TYPE'] == "A" )
    {
        echo "Success 1";
    }
    else if ($row['E_TYPE'] == "B" )
    {
        echo "Success 2";
    }
    else {
        echo "Error...";
    }

}

#2


1  

mssql_query return MS SQL result set, to get returned value, you have to fetch row from this resultset first. See http://www.php.net/manual/en/function.mssql-fetch-assoc.php and other mssql_fetch functions

mssql_query返回MS SQL结果集,要获取返回值,必须先从此结果集中获取行。请参阅http://www.php.net/manual/en/function.mssql-fetch-assoc.php和其他mssql_fetch函数

#3


0  

<?php
$server = -Removed;
$login = -Removed;           
$pass = -Removed;        
$login=$_POST['login'];
$password=$_POST['password'];
$table= 'USERPASS';
$table2='EMPLOYEES';
$res='q';

$dblink = @mssql_connect(-Removed) or die("Error 1");
mssql_select_db('group5', $dblink) or die( "unable to select the database");
$sqlquery = "SELECT E_TYPE FROM USERPASS U, EMPLOYEES E WHERE U.EMPLOYEE_ID = E.EMPLOYEE_ID AND PASSWORD = '$password' AND USERNAME = '$login'";
$res = mssql_query($sqlquery, $dblink) or die("Error5");
$count=mssql_num_rows($res);
    if(1 == $count){
        $row=mssql_fetch_assoc($res);//fetch row from database
        switch($row['E_TYPE']) {
            case "A":
                echo "Success 1"; 
                break;
            case "B":
                echo "Success 2"; 
                break;
            default:
                echo "Error..."; 
                break;
        }
    }
}
mssql_free_result($res);
?>

you should learn about select...case statements. only thing I can see is the case of your characters. databases may or may not be case sensitive. you are also missing something which can sometimes affect your results: mysql_free_result(). you should usually do a while statement with regards to fetching rows rather than an if, because there may be more than 1 row.

你应该了解select ... case语句。我唯一能看到的是你角色的情况。数据库可能是也可能不区分大小写。你也遗漏了有时会影响结果的东西:mysql_free_result()。你应该通常做一个关于获取行而不是if的while语句,因为可能有超过1行。

I usually put the constant before the comparison operator and the variable on the right side when it's possible and it makes sense, because the compiler will catch the error if in any case it becomes an assignment because of a missing =.

我通常在比较运算符之前将常量和右侧的变量放在可能的情况下并且它是有意义的,因为如果在任何情况下由于缺少=而成为赋值,编译器将捕获错误。