将 PHP 代码从 MySql 转换为 Sqlsrv 时出现代码问题

trouble with code when converting PHP code from MySql to Sqlsrv

我已经从 MySql 转移到 Sqlsrv 并正在更新我的 PHP。我不是那么精通 PHP,绝对不是 Sqlsrv,但一直在使用 PHP:SQLSRV 手册。 这是我的 PHP 代码:

<?php
include '../includes/dbconfig.php';
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {

// username and password sent from Form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
//extract($_POST);

$sql="SELECT username FROM dbname WHERE username='$myusername' and userpassword='$mypassword'";
$result=sqlsrv_query($conn,$sql) or die(print_r(sqlsrv_errors(),true));

if ($result) {
   $rows = sqlsrv_has_rows( $result );
   if ($rows === true)
        echo "There are rows. <br />";
    {
        $_SESSION['login_user']=$myusername;
        header("Location: orderform.php");
    }
   else 
    { $error="Your Login Name or Password is invalid"; }
}
}
?>

导致问题的代码是:

{
$_SESSION['login_user']=$myusername;
header("Location: orderform.php");
}

如果我注释掉这些行,登录表单就会出现,如果我输入正确的用户名和密码,我会得到 "There are rows" 注释。如果没有,我得到 "Your Login Name or Password is invalid"。一旦我把这些线放回去,登录屏幕就不会出现了。我没有得到错误或任何东西。我已经为此工作了 2 天......有人可以帮助我吗?

我认为你的括号结构有误:

   if ($rows === true)
        echo "There are rows. <br />";
    {
        $_SESSION['login_user']=$myusername;
        header("Location: orderform.php");
    }
   else 
    { $error="Your Login Name or Password is invalid"; }

应该是:

   if ($rows === true)
    {
        echo "There are rows. <br />";
        $_SESSION['login_user']=$myusername;
        header("Location: orderform.php");
    }
   else 
    { $error="Your Login Name or Password is invalid"; }